check true false in php code example

Example 1: true false php

""         -> 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

Example 2: php check if variable is true or false

Testing $var == true is the same than just testing $var.

You can read this SO question on comparison operator. You can also read PHP manual on this topic.

Note: a variable does not return true. It is true, or it evaluates to true. However, a function returns true.

Tags:

Php Example