PHP type comparison tables

The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at » BlueShoes.

Before utilizing these tables, it's important to understand types and their meanings. For example, "42" is a string while 42 is an int. false is a bool while "false" is a string.

Note:

HTML Forms do not pass integers, floats, or booleans; they pass strings. To find out if a string is numeric, you may use is_numeric().

Note:

Simply doing if ($x) while $x is undefined will generate an error of level E_NOTICE. Instead, consider using empty() or isset() and/or initialize your variables.

Note:

Some numeric operations can result in a value represented by the constant NAN. Any loose or strict comparisons of this value against any other value, including itself, but except true, will have a result of false. (i.e. NAN != NAN and NAN !== NAN) Examples of operations that produce NAN include sqrt(-1), asin(2), and acosh(0).

Comparisons of $x with PHP functions
Expression gettype() empty() is_null() isset() bool : if($x)
$x = ""; string true false true false
$x = null; NULL true true false false
var $x; NULL true true false false
$x is undefined NULL true true false false
$x = []; array true false true false
$x = ['a', 'b']; array false false true true
$x = false; bool true false true false
$x = true; bool false false true true
$x = 1; int false false true true
$x = 42; int false false true true
$x = 0; int true false true false
$x = -1; int false false true true
$x = "1"; string false false true true
$x = "0"; string true false true false
$x = "-1"; string false false true true
$x = "php"; string false false true true
$x = "true"; string false false true true
$x = "false"; string false false true true

Loose comparisons with ==
true false 1 0 -1 "1" "0" "-1" null [] "php" ""
true true false true false true true false true false false true false
false false true false true false false true false true true false true
1 true false true false false true false false false false false false
0 false true false true false false true false true false false* false*
-1 true false false false true false false true false false false false
"1" true false true false false true false false false false false false
"0" false true false true false false true false false false false false
"-1" true false false false true false false true false false false false
null false true false true false false false false true true false true
[] false true false false false false false false true true false false
"php" true false false false* false false false false false false true false
"" false true false false* false false false false true false false true
* true prior to PHP 8.0.0.

Strict comparisons with ===
true false 1 0 -1 "1" "0" "-1" null [] "php" ""
true true false false false false false false false false false false false
false false true false false false false false false false false false false
1 false false true false false false false false false false false false
0 false false false true false false false false false false false false
-1 false false false false true false false false false false false false
"1" false false false false false true false false false false false false
"0" false false false false false false true false false false false false
"-1" false false false false false false false true false false false false
null false false false false false false false false true false false false
[] false false false false false false false false false true false false
"php" false false false false false false false false false false true false
"" false false false false false false false false false false false true

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top