If I return nothing explicitly, what does a php function exactly return?
function foo() {}
What type is it?
What value is it?
How do I test for it exactly with === ?
Did this change from php4 to php5?
Is there a difference between function foo() {}
and function foo() { return; }
(I am not asking how to test it like if (foo() !=0) ...
)
string when no string is returned.
PHP Functions returning value return stops the execution of the function and sends the value back to the calling code. You can return more than one value from a function using return array(1,2,3,4). Following example takes two integer parameters and add them together and then returns their sum to the calling program.
The default return value from a function is int. Unless explicitly specified the default return value by compiler would be integer value from function.
PHP functions do not need to return anything, and I doubt it would negatively affect the performance if you didn't return anything. If anything, it would positively affect the performance.
null
null
if(foo() === null)
You can try it out by doing:
$x = foo();
var_dump($x);
Not returning a value from a PHP function has the same semantics as a function which returns null.
function foo() {}
$x=foo();
echo gettype($x)."\n";
echo isset($x)?"true\n":"false\n";
echo is_null($x)?"true\n":"false\n";
This will output
NULL
false
true
You get the same result if foo is replaced with
function foo() {return null;}
There has been no change in this behaviour from php4 to php5 to php7 (I just tested to be sure!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With