I was afraid someone would say this I always try to make something as simple as possible, with as few ifs as possible. to convert a number to a boolean, as if it was an explicit (bool) cast then NOT. var_dump((bool) "1"); //evaluates to true o llame a la función boolval : Here's how to do it. ""); outputs: boolean true boolean false boolean true boolean false May the life be good to you. It just happens to cast to integer 1 when you print it or use it in an expression, but it's not the same as a constant for the integer value 1 and you shouldn't use it as one. it's a binary thing.

!1, ! I was told that this is a feature, not a bug. //array_search returns index of element, and FALSE if nothing is found//but any non-existent element will also remove 'A'! Para convertir un tipo en booleano, puede usar la conversión (bool) o (boolean) antes del tipo. "\n"; // prints "\n" ?> Why doesn't php -f test.php print TRUE or FALSE? I believe that the double negation !!

Beware of certain control behavior with boolean and non boolean values :// Consider that the 0 could by any parameters including itself

:o (since it used to be a bit and then there was only 0 and 1 as an option) You will have to use an if. Notice what it says at the top of the page: Just a side note, doesn't really matters, the reason -1 is true and not false is because boolean type is treated as unsigned, so -1 would be for example, if it's unsigned int32 translate to hex: 0xFFFFFFFF and back to decimal: 4294967295 which is non-zero. The value true is also not a constant for 1, it is a special boolean value that indicates true. PHP does not break any rules with the values of true and false. there isn't really a "negative boolean". Note that the comparison: (false == 0) evaluates to true and so will any value you set to false as well (without casting). Note you can also use the '!' Ah, yes, booleans - bit values that are either set (TRUE) or not set (FALSE). What will be the output you think if we add echo and print both at a time..// prints out: 51     inner 'print' prints the value of $a, then                                                                                                                                     // outer 'print' returns  the boolean true about print $a// prints out: 51     inner 'print' prints the value of $a, then

!0, !!

It appears there's a lot more truth in this universe, but false can trump anything that's true... Note that the symbolic constants TRUE and FALSE are treated differently. Dunno if someone else posted this solution already, but if not, here's a useful and function to convert strings to strict booleans. echo también posee una sintaxis abreviada, donde se puede poner el símbolo igual justo después de la etiqueta de apertura de PHP. PHP often tries to autoconvert these strings to numeral, as the programmer certainly intended (try 'echo "2"+3'). Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP (taken from the documentation for empty): "" (an empty string); "0" (0 as a string) If you need to set a boolean based on the text value of a string, then you'll need to check for the presence or otherwise of that value.

Consequently, PHP designers decided to treat 0 and "0" similarly, ie. Antes de PHP 5.4.0, este tipo de sintaxis abreviada únicamente funciona con la opción de configuración short_open_tag activada. "\n"; // prints "1\n" echo FALSE . It is correct that TRUE or FALSE should not be used as constants for the numbers 0 and 1. "test", !! PHP - Get bool to echo false when false. falsy, for consistency and to avoid bugs where the programmer believes he got a true … Function to sort array by elements and count of element (before php 5.3) (not use Lambda Functions, and Closures);                                                  Actually from a complete noob point of view 0 resulting in false makes sense as many languages as I have been taught consider the value 1 as true and the value 0 as false a simple boolean value. in Php. Casting bools to string is not working as maybe expected: Just something that will probably save time for many new developers: beware of interpreting FALSE and TRUE as integers. More importantly, in the FALSE case, why doesn't it print anything? But there may be times when it might be helpful to see the value of the Boolean as a 1 or 0. When compare with TRUE with also the same string it's also returning true. Now that we have 64 bit compilers using an int variable for booleans, there is *one* value which is FALSE (zero) and 2**64-1 values that are TRUE (everything else). To echo a boolean value, we can use the ternary operator (?) For those wondering why the string "0" is falsy, consider that a good deal of input data is actually string-typed, even when it is semantically numeral. can perform the same task with the same result if your PHP is not up2date var_dump(! When compare zero (0) with string it's return true by type jugling. Los booleanos se pueden usar en sentencias como esta: Debido al hecho de que PHP está escrito de forma débil, si Para evitar esta comparación, puede imponer una comparación sólida utilizando Para convertir un tipo en booleano, puede usar la conversión Conversión booleana a una cadena (tenga en cuenta que Tenga en cuenta que lo contrario también es posible: También todo lo que no sea cero devolverá verdadero: This modified text is an extract of the original Stack Overflow Documentation created by following Given the following test.php: