Ah, yes, booleans - bit values that are either set (TRUE) or not set (FALSE). 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). It appears there's a lot more truth in this universe, but false can trump anything that's true...
PHP's handling of strings as booleans is *almost* correct - an empty string is FALSE, and a non-empty string is TRUE - with one exception: A string containing a single zero is considered FALSE. Why? If *any* non-empty strings are going to be considered FALSE, why *only* a single zero? Why not "FALSE" (preferably case insensitive), or "0.0" (with how many decimal places), or "NO" (again, case insensitive), or ... ?
The *correct* design would have been that *any* non-empty string is TRUE - period, end of story. Instead, there's another GOTCHA for the less-than-completely-experienced programmer to watch out for, and fixing the language's design error at this late date would undoubtedly break so many things that the correction is completely out of the question.
Speaking of GOTCHAs, consider this code sequence:
<?php
$x=TRUE;
$y=FALSE;
$z=$y OR $x;
?>
Is $z TRUE or FALSE?
In this case, $z will be FALSE because the above code is equivalent to <?php ($z=$y) OR $x ?> rather than <?php $z=($y OR $x) ?> as might be expected - because the OR operator has lower precedence than assignment operators.
On the other hand, after this code sequence:
<?php
$x=TRUE;
$y=FALSE;
$z=$y || $x;
?>
$z will be TRUE, as expected, because the || operator has higher precedence than assignment: The code is equivalent to $z=($y OR $x).
This is why you should NEVER use the OR operator without explicit parentheses around the expression where it is being used.⚡ NEW: Manual/en/language.types.boolean.php - HD Photos!
Booleans
The bool type only has two values, and is used to express
a truth value. It can be either true or false.
Syntax
To specify a bool literal, use the constants true or
false. Both are case-insensitive.
<?php
$foo = True; // assign the value TRUE to $foo
?>Typically, the result of an operator which returns a bool value is passed on to a control structure.
<?php
$action = "show_version";
$show_separators = true;
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}
// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}
// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>Converting to boolean
To explicitly convert a value to bool, use the
(bool) cast. Generally this is not necessary because when
a value is used in a logical context it will be automatically interpreted
as a value of type bool. For more information see the
Type Juggling page.
When converting to bool, the following values are considered
false:
-
the boolean
falseitself -
the integer
0(zero) -
the floats
0.0and-0.0(zero) -
the empty string
"", and the string"0" - an array with zero elements
- the unit type NULL (including unset variables)
- Internal objects that overload their casting behaviour to bool. For example: SimpleXML objects created from empty elements without attributes.
Every other value is considered true
(including resource
and NAN).
-1 is considered true, like any other non-zero
(whether negative or positive) number!
Example #1 Casting to Boolean
<?php
var_dump((bool) ""); // bool(false)
var_dump((bool) "0"); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
?>User Contributed Notes 12 notes
Note for JavaScript developers:
In PHP, an empty array evaluates to false, while in JavaScript an empty array evaluates to true.
In PHP, you can test an empty array as <?php if(!$stuff) …; ?> which won’t work in JavaScript where you need to test the array length.
This is because in JavaScript, an array is an object, and, while it may not have any elements, it is still regarded as something.
Just a trap for young players who routinely work in both langauges.Just something that will probably save time for many new developers: beware of interpreting FALSE and TRUE as integers.
For example, a small function for deleting elements of an array may give unexpected results if you are not fully aware of what happens:
<?php
function remove_element($element, $array)
{
//array_search returns index of element, and FALSE if nothing is found
$index = array_search($element, $array);
unset ($array[$index]);
return $array;
}
// this will remove element 'A'
$array = ['A', 'B', 'C'];
$array = remove_element('A', $array);
//but any non-existent element will also remove 'A'!
$array = ['A', 'B', 'C'];
$array = remove_element('X', $array);
?>
The problem here is, although array_search returns boolean false when it doesn't find specific element, it is interpreted as zero when used as array index.
So you have to explicitly check for FALSE, otherwise you'll probably loose some elements:
<?php
//correct
function remove_element($element, $array)
{
$index = array_search($element, $array);
if ($index !== FALSE)
{
unset ($array[$index]);
}
return $array;
}