Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between returning void and null in PHP? [duplicate]

Tags:

php

null

void

When I have a function like this:

public function getResult() {
    return;
}

Is it the exactly same as returning null? And if I'd be doing this:

is_null($this->getResult());

Would that result in true?

like image 390
Sander Avatar asked Sep 06 '25 21:09

Sander


1 Answers

In terms of runtime behaviour, no, there is not. All functions implicitly return a NULL, and return with no value (so return;) produces a NULL return value.

If you use the : void return type declaration in PHP 7.1+, return NULL; (instead of return;) is forbidden, but this is really just a coding-style restriction. Function calls always produce some value in PHP, so var_dump((function (): void {})()); outputs NULL.

like image 188
Andrea Avatar answered Sep 08 '25 10:09

Andrea