Surprisingly enough I don't get a parse error from this: if (is_bool('a' || 10)) echo 'Is boolean'; PHP: This echos "Is boolean". Whats happening here? PHP 5.2.3
Both values will be casted to boolean, and both would be true. Let's rewrite it so you understand it better. $foo = 'a'; // 'a' casted to boolean is true. (bool)'a' === true $bar = 10; // 10 casted to boolean is true too. (bool)10 === true // When you're doing this, PHP will cast both values to boolean and operate with the given operator. $bool = 'a' || 10; // OR: If 'a' or 10 is True - Return true // $bool is now boolean True, so: if (is_bool($bool)) echo 'Is boolean'; // ... is true.. and the text is outputted. PHP: This syntax can be useful for thing like this: if ($foo == 'a' OR $bar == 10) { $bool = true; } else { $bool = false; } // The above can be simplified to: $bool = $foo == 'a' OR $bar == 10; // $bool is now boolean true PHP:
Wow, that helps. I didn't really think of it that way. So PHP is converting the parameter before the function looks at it. Thats what was confusing me. This is a bit more apparent: is_bool(('a' || 10)) Thanks!