Is one of the following functions better than the other, in terms of where to place the 'return false' statement?
Function #1:
function equalToTwo($a, $b)
{
$c = $a + $b;
if($c == 2)
{
return true;
}
return false;
}
Function #2:
function equalToTwo($a, $b)
{
$c = $a + $b;
if($c == 2)
{
return true;
}
else
{
return false;
}
}
Thanks!
return $oh || false does not work in PHP like it works in JavaScript. It would always return a boolean value (true or false). – Matthew. Apr 13, 2011 at 15:15. $result = $oh OR false will work as expected, since OR has a lower precedence than the return (but the second option must be a boolean).
The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.
return 1 in the main function means that the program does not execute successfully and there is some error. return 0 means that the user-defined function is returning false. return 1 means that the user-defined function is returning true.
A function can not return multiple values, but similar results can be obtained by returning an array.
There is no functional difference between the two; you should choose whichever one is most obvious and readable.
I would usually use an else
.
Note that your particular example should be written as
return $c == 2;
What about just:
return ($c == 2);
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