It's not very important but I was just curious to know the difference.
echo isA("A"); //outputs 1
echo isA("B"); //outputs nothing. why doesn't it output 0?
Anybody can shed somelight on this matter? It does seem to me as a double standard when you look at it from the point of view that "true" outputs as "1" but "false"does not output "0".
Again, no big deal but I think there must be a reason for PHP to be designed like that. Knowing that may give some more insight into this beautiful language.
A true value will manifest itself as a visible 1, but a false value won't. So, tell me what's the advantage of this method?
example function I referred above;
function isA($input){
if ( $input == "A" ):
return true;
else:
return false;
endif;
}
The values 1 and 0 are of type int and are not implicitly convertible to boolean . So when you return 1, you are basically returning True as the final value of the function while return 0 is basically returning False as the final value of the function.
returning true or false indicates that whether execution should continue or stop right there. So just an example <input type="button" onclick="return func();" /> Now if func() is defined like this function func() { // do something return false; } the click event will never get executed.
C has no concept of boolean other than 0 being false, and anything else being true. The idea of returning 0 comes from having a return code indicating what the failure is. If you're not doing a return code, then there's nothing wrong with treating 1 and 0 as true and false.
A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.
http://us3.php.net/manual/en/language.types.string.php#language.types.string.casting
If you want to print a boolean for debugging you can use var_dump or print_r.
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