Return true outputs 1, but return false outputs

This is not very important, but I was just interested to know the difference.

echo isA("A"); //outputs 1 echo isA("B"); //outputs nothing. why doesn't it output 0? 

Can anyone shed some light on this question? This seems to me a double standard when you look at it from the point of view that the β€œtrue” outputs are like β€œ1” but β€œfalse” do not output β€œ0”.

Again, it doesn’t matter, but I think this approach should be like this for PHP. Knowing this can provide a deeper understanding of this beautiful language.

The true value will appear as visible 1, but the false value will not. So tell me what is the advantage of this method?

exemplary function mentioned above;

 function isA($input){ if ( $input == "A" ): return true; else: return false; endif; } 
+6
source share
2 answers

The boolean value TRUE is converted to the string "1". Boolean FALSE - This is converted to "" (empty string). This allows 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.

+11
source

Because when false sent to a string, it becomes '' - an empty string.

To see the difference, use var_dump(); instead of echo

 var_dump((string) true); var_dump((string) false); 
+5
source

Source: https://habr.com/ru/post/911058/


All Articles