PeeHaa PeeHaa - 2 months ago 5
PHP Question

Should a function use: return null;?

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.

Answer

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.