68

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 ?

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
Moon
  • 22,195
  • 68
  • 188
  • 269

6 Answers6

132

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.

andras.tim
  • 1,964
  • 1
  • 13
  • 23
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 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. – kungphu May 09 '16 at 03:41
  • 2
    Note 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. – TextGeek May 12 '16 at 18:05
  • 2
    You 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 `==`. – Exadra37 Oct 01 '19 at 13:16
22

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 except true returns false.
  • 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)
akinuri
  • 10,690
  • 10
  • 65
  • 102
11

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
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • why `0 === false` won't work? Also is there a difference between `0 === false` and `0 === FALSE`? – JavaSa Jan 06 '15 at 12:07
  • 1
    There is no difference between `0 === false` and `0 === 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. – kungphu May 09 '16 at 03:44
4

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.

Citizen
  • 12,430
  • 26
  • 76
  • 117
2

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

sidereal
  • 1,072
  • 7
  • 15
  • 8
    That "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 ;-) – Pascal MARTIN Mar 04 '10 at 20:36
  • 3
    Don'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 – sidereal Mar 04 '10 at 21:12
  • 1
    Yeah... 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. – spronkey Mar 04 '10 at 22:04
2

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.

Anycorn
  • 50,217
  • 42
  • 167
  • 261