58

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.

5
  • If you have a practice of indicating that null value means something specific, i.e. function arguments contain bad data - then go ahead. But often boolean return is more than enough, though. I use null in anonymous funcs that are expected to return actual data, so boolean false is considered an acceptable return, but null is not. Commented Jan 5, 2012 at 20:17
  • 1
    We can conclude that it is redundant, as the runtime already does that. But that doesn't make it bad or unuseful. The answer is explicitness. More interestingly, shouldn't you write NULL? Commented Jan 5, 2012 at 20:19
  • @mario you mean null vs NULL? Commented Jan 5, 2012 at 20:20
  • 2
    @PeeHaa: Yes, I believe the uppercase variant is the canonical token. And going back to explicitness, it has a bigger announcement effect. Commented Jan 5, 2012 at 20:21
  • 1
    Isn't not writing return even more explicit here? Commented Jan 5, 2012 at 20:25

7 Answers 7

69

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
Sign up to request clarification or add additional context in comments.

5 Comments

The last function example is very clear about the point that there is nothing to return. So which example shows best in your eye?
As in first comment... Always is a good practice to show what you're returning. So my choice is first one.
So first you show that you return something to then finish the sentence to say that that something is NULL instead making less words and therefore stating the obvious?
I would go with the following approach for semantic meaningful programming: dereuromark.de/2015/10/05/return-null-vs-return-void
$foo = NULL; print $foo; will NOT throw an error but print $foo; will.
41

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.

4 Comments

True, but this way it's more clear that the return value will probably be used by the caller.
Unless there isn't any documentation, I find it hard to say. What do you read first? The last line of the function body or the return type docs just in it's head?
Not every project uses doc comments.
Not every function returns something. Or should the code here clearly document that NULL is to be returned for those who don't speak the language? Okay, I see the answer changed, so I think it's clear.
13

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

1 Comment

Presumably this is because, since PHP 7.1, 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.)
11

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.

Comments

10

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
}

Comments

1

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.

1 Comment

That's true. I just like being verbose, I guess. When I'm looking at my code 1 year from now, late at night, trying to figure out what's broken, I like having everything in front of me (nothing is implied). Personal preference.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.