Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning true outputs 1 but returning false outputs nothing

Tags:

php

boolean

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;
}
like image 741
Average Joe Avatar asked Mar 19 '12 02:03

Average Joe


People also ask

Is return 0 and return false the same?

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.

What is the meaning of return true or false?

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.

What does return false mean in C?

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.


1 Answers

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.

like image 118
takteek Avatar answered Nov 13 '22 09:11

takteek