Should a function return null?
E.g.
function test()
{
return null; // vs return;
}
Is the latter considered bad practice or doesn't it matter?
PS
Whether it is bad practice shouldn't be subjective IMHO.
Always is a good practice to show what you're returning.
But as a tip, the following are all equivalent:
function test()
{
return null;
}
function test()
{
return;
}
function test()
{
}
In all cases there for var_dump(test()); will be:
NULL
NULL instead making less words and therefore stating the obvious?If you don't return anything, just use return; or omit it at all at the end of the function.
If your function is usually returns something but doesn't for some reason, return null; is the way to go.
That's similar to how you do it e.g. in C: If your function doesn't return things, it's void, otherwise it often return either a valid pointer or NULL.
When you use return type declarations with the nullable prefix "?" there is a difference!
The PHP documentation says ...
As of PHP 7.1.0, return values can be marked as nullable by prefixing the type name with a question mark (?). This signifies that the function returns either the specified type or NULL.
So, when you return nothing the error message will be:
Fatal error: Uncaught TypeError: Return value of test() must be of the type string or null, none returned in ...
And when you return return; the error message will be:
Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?) in ...
When you return null, everything is fine.
void is a special return type (for functions which do not return a value), and now distinct from null (which is the value null) when it comes to comes to function return values/types. Though if you tried to assign the return value to a variable in either case your variable would end up with the value null. (See also the PHP RFC for void return type.)An undefined function return value in PHP always equals NULL, so it does not make any difference (the runtime does this).
What makes a difference is that you make use of docblocks and document the return value with the @return tag so clever IDE's offer info here.
If you want to signal per the docblock that not-returning a value is intended, you can use void:
* @return void
If you want to signal that returning NULL is intended, you can use null or NULL (depending on your code-style for uppercase PHP standard constants):
* @return null
or:
* @return NULL
That should make the coders intention visible as by PHP this is would be always null as the factual return value.
Read on:
More details and updated information is in PeeHaa's answer to this same question.
It is just plain silly to return null if there is nothing to return. Doing return; shows intend of not actually returning anything. And makes it clear what the function does.
The fact that PHP does not have a void return type yet should not be a reason to make the intend less clear. Also note that PHP 7.1 will provide support for the void return type:
function test(): void {
return null; // this will error
}
Semantically, I don't think there's a difference between the two. According to the PHP documentation on return:
If no parameter is supplied, then the parentheses must be omitted and NULL will be returned.
I personally like putting the NULL there. Then there's no question as to what's being returned, making your debugging a little easier.
The PEAR best practices page does a return null in their "Return early" example. This increases readability which we should always try to do since we write code for humans, not computers.
source: http://pear.php.net/manual/en/standards.bestpractices.php
nullvalue means something specific, i.e. function arguments contain bad data - then go ahead. But often boolean return is more than enough, though. I usenullin anonymous funcs that are expected to return actual data, so booleanfalseis considered an acceptable return, butnullis not.NULL?nullvsNULL?