Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a function use: return null;?

Tags:

php

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.

like image 686
PeeHaa Avatar asked Jan 05 '12 20:01

PeeHaa


People also ask

Should I return null or undefined?

Generally, if you need to assign a non-value to a variable or property, pass it to a function, or return it from a function, null is almost always the best option. To put it simply, JavaScript uses undefined and programmers should use null .

Is it better to return null or empty values from functions methods where the return value is not present?

Regardless of whether or not NULL or an empty Person Object ( new Person() ) is returned the caller is going to have to check to see if the Person Object is NULL or empty before doing anything to it (like calling UpdateName() ). So why not just return NULL here and then the caller only has to check for NULL.

Should I return null or empty array?

An empty array actually means something, as does null. To say that you should always return an empty array rather than null, is almost as misguided as saying you a boolean method should always return true. Both possible values convey a meaning. You should actually prefer returning System.

Can any function return null?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type. In the absence of a constructor, the getArticles() and getName() methods will return a null reference.


6 Answers

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 
like image 139
devdRew Avatar answered Oct 03 '22 11:10

devdRew


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.

like image 36
ThiefMaster Avatar answered Oct 03 '22 10:10

ThiefMaster


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
}
like image 42
PeeHaa Avatar answered Oct 03 '22 10:10

PeeHaa


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.

like image 31
hakre Avatar answered Oct 03 '22 12:10

hakre


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.

See https://3v4l.org/5NTvg

like image 41
powtac Avatar answered Oct 03 '22 12:10

powtac


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.

like image 45
Jonah Bishop Avatar answered Oct 03 '22 12:10

Jonah Bishop