I wonder how PHP handles true/false comparison internally.
I understand that true is defined as 1 and false is defined as 0.
When I do if("a"){ echo "true";}
it echos "true". How does PHP recognize "a" as 1 ?
-
5It's overly simplistic to say that "true is defined as 1 and false is defined as 0."– Matt BallMar 4, 2010 at 20:33
-
3btw. guess what this returns: "false" == true– Pavel NiedobaNov 4, 2016 at 20:59
6 Answers
This is covered in the PHP documentation for booleans and type comparison tables.
When converting to boolean, the following values are considered FALSE:
- the boolean
FALSE
itself - the integer
0
(zero) - the float
0.0
(zero) - the empty string, and the string
'0'
- an array with zero elements
- an object with zero member variables (PHP 4 only)
- the special type
NULL
(including unset variables) - SimpleXML objects created from empty tags
Every other value is considered TRUE.
-
1"the programmers community" != sh != bash, etc. And no programming or scripting language I've ever used has the convention that anything non-zero is false (of course this doesn't mean there isn't one, but it's a pretty decent sample set). While I definitely prefer strong typing and can't stand much at all about PHP, 'truthy' and 'falsey' values are very common in languages that allow comparison between what would otherwise be incomparable types, and how truthitude is determined for a given type in a given language can be pretty arbitrary.– kungphuMay 9, 2016 at 3:41
-
2Note that when @MarkByers said 'the string "0"', that's exactly right. For example, the string "00", the string "0.0", "\0", etc. do not similarly come out false.– TextGeekMay 12, 2016 at 18:05
-
2You should add to your answer that to check if a var is really a boolean, the strict comparison must be used with the
===
operator, not the common used one==
.– Exadra37Oct 1, 2019 at 13:16
Because I've visited this page several times, I've decided to post an example (loose) comparison test.
Results:
"" -> false
"0" -> false
"0.0" -> true
"1" -> true
"01" -> true
"abc" -> true
"true" -> true
"false" -> true
"null" -> true
0 -> false
0.1 -> true
1 -> true
1.1 -> true
-42 -> true
"NAN" -> true
0 -> false
NAN -> true
null -> false
true -> true
false -> false
[] -> false
[""] -> true
["0"] -> true
[0] -> true
[null] -> true
["a"] -> true
{} -> true
{} -> true
{"t":"s"} -> true
{"c":null} -> true
Test code:
class Vegetable {}
class Fruit {
public $t = "s";
}
class Water {
public $c = null;
}
$cases = [
"",
"0",
"0.0",
"1",
"01",
"abc",
"true",
"false",
"null",
0,
0.1,
1,
1.1,
-42,
"NAN",
(float) "NAN",
NAN,
null,
true,
false,
[],
[""],
["0"],
[0],
[null],
["a"],
new stdClass(),
new Vegetable(),
new Fruit(),
new Water(),
];
echo "<pre>" . PHP_EOL;
foreach ($cases as $case) {
printf("%s -> %s" . PHP_EOL, str_pad(json_encode($case), 10, " ", STR_PAD_RIGHT), json_encode( $case == true ));
}
Summary:
- When a strict (
===
) comparison is done, everything excepttrue
returnsfalse
. - an empty string (
""
) is falsy - a string that contains only
0
("0"
) is falsy NAN
is truthy- an empty array (
[]
) is falsy - a container (array, object, string) that contains a falsy value is truthy
- an exception to this is
0
in""
(see the third item)
- an exception to this is
-
["false"] -> true, been chewing my foot off over that one today. May 25, 2022 at 22:50
-
Zero is false, nonzero is true.
In php you can test more explicitly using the ===
operator.
if (0==false)
echo "works"; // will echo works
if (0===false)
echo "works"; // will not echo anything
-
why
0 === false
won't work? Also is there a difference between0 === false
and0 === FALSE
?– JavaSaJan 6, 2015 at 12:07 -
1There is no difference between
0 === false
and0 === FALSE
. Both falses evaluate to the same value; for function names and language keywords, PHP is, obnoxiously, case-insensitive. Both of these "work," but they evaluate to false, because===
actually does a type-dependent value check.==
allows checks between types; see the accepted answer on this thread for how PHP determines what matches false and what doesn't.– kungphuMay 9, 2016 at 3:44
The best operator for strict checking is
if($foo === true){}
That way, you're really checking if its true, and not 1 or simply just set.
PHP uses weak typing (which it calls 'type juggling'), which is a bad idea (though that's a conversation for another time). When you try to use a variable in a context that requires a boolean, it will convert whatever your variable is into a boolean, according to some mostly arbitrary rules available here: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
-
8That "bad idea" is purely subjective : once you know how this works, it is absolutly not a problem, in my opinion ;-) ;; and the conversion rules mostly don't seem quite un-logical to me ;-) Mar 4, 2010 at 20:36
-
3Don't make me post a stackoverflow question about how bad an idea weak typing is ;). Consider that php manages to make a basic syllogism fail, because implicit type conversions make php equality non-transitive: echo ("true" == true)."\n"; //true echo (true == 1)."\n"; // true echo ("true" == 1)."\n"; // false :P– siderealMar 4, 2010 at 21:12
-
1Yeah... it's a (potentially) very dangerous trick for new players, but not necessarily a bad idea. I'm not going to defend the robustness of code here, but sometimes it is a nice shortcut. Besides, if you're doing much in the way of larger scale work you'll probably use equality methods most of the time instead.– spronkeyMar 4, 2010 at 22:04
think of operator as unary function: is_false(type value)
which returns true or false, depending on the exact implementation for specific type and value. Consider if statement to invoke such function implicitly, via syntactic sugar.
other possibility is that type has cast operator, which turns type into another type implicitly, in this case string to Boolean.
PHP does not expose such details, but C++ allows operator overloading which exposes fine details of operator implementation.