2

Consider a function that explicitly returns null:

function nullreturn() {return null;}
$rv = nullreturn();
var_dump($rv);
var_dump(isset($rv));
var_dump(is_null($rv));

Which identifies as null -- as expected.

NULL
bool(false)
bool(true)

And consider a function with no return value -- or even a return statement:

function noreturn() {}
$rv = noreturn();
var_dump($rv);
var_dump(isset($rv));
var_dump(is_null($rv));

Which also identifies as null:

NULL
bool(false)
bool(true)

... is there a way to determine that noreturn returns "nothing" instead of null?


As to why I need this null/void distinction, I've just been trying to achieve compatibility with a previous service implementation that did make this distinction and which includes tests for it. But, it's probably not critical. I just don't want to strip away the relevant tests and hope I wasn't depending on the distinction if I were overlooking an achievable solution.

7
  • 8
    You can't, because (as documented) no return value means a return of null; not unless the return void RFC is accepted for PHP 7.1 Commented Jun 15, 2016 at 22:46
  • I'm more interested in what lead you to ask this question Commented Jun 15, 2016 at 22:52
  • Nothing returned will just lead to NULL being returned. Any reason why you would need to differentiate between the 2? Commented Jun 15, 2016 at 22:52
  • I'm suggest it's a function not procedure. If you remember pascal-language. It has two major types of subroutines - function that returns a typed value and procedure that returns nothing. It hides inside the word function. sin is math-function, cos is math-function - both of them returns a value. PHP doesn't have any other major types of subroutines. :).. In any case function is function, it must returns a value, imho. I think this text is kind of offtop .. sry :) Commented Jun 15, 2016 at 23:24
  • Do you know which functions have no return? If your function is part of a class, I'd set a variable within the class to a value you'll recognize as "no return". Assuming the two functions you're trying to differentiate are not within the same class. Commented Jun 15, 2016 at 23:49

1 Answer 1

5

No. Not returning, and returning NULL, are the exact same thing. A function with no return value implicitly returns NULL. This applies in many languages. Sorry if this isn't the answer you're looking for, but it is reality.

The following each do the exact same thing:

function A() {
    return;
}

function B() {
    return NULL;
}

function C() {
}

See also return in the PHP manual:

Note: If no parameter is supplied, then the parentheses must be omitted and NULL will be returned. Calling return with parentheses but with no arguments will result in a parse error.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.