PeeHaa PeeHaa - 1 year ago 83
PHP Question

Should a function use: return null;?

Should a function return



function test()
return null; // vs return;

Is the latter considered bad practice or doesn't it matter?


Whether it is bad practice shouldn't be subjective IMHO.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download