You shouldn't, I would always use return null;
so that it is an explicit declaration of what is returned (even if it is null
). I'm sure this is also in one of the PSR specs as well, but I don't know them well. To confirm that return; === return null;
:
function test() {
return;
}
var_dump(test());
// NULL
As to when you would want to return null;
..any function that returns something, should always return something. So maybe if you have a function that gathers and returns a value from a DB, and an error occurs:
public function retrieveData()
{
$data = DB::retrieve();
if(!$data) {
return null;
}
return $data;
}
However, a lot of functions that may have errors just return true
/false
on success or failure so you won't necessarily use this often.
My main point to drive home: if you don't want/need to return anything, don't return anything.