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 integer. FALSE is a
boolean 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).

A comparison table for <=,<,=>,> would be nice...Following are TRUE (tested PHP4&5):NULL <= -1NULL <= 0NULL <= 1!(NULL >= -1)NULL >= 0!(NULL >= 1)That was a surprise for me (and it is not like SQL, I would like to have the option to have SQL semantics with NULL...).

In some languages, a boolean is promoted to an integer (with a value of 1 or -1, typically) if used in an expression with an integer. I found that PHP has it both ways:

If you add a boolean with a value of true to an integer with a value of 3, the result will be 4 (because the boolean is cast as an integer).

On the other hand, if you test a boolean with a value of true for equality with an integer with a value of three, the result will be true (because the integer is cast as a boolean).

Surprisingly, at first glance, if you use either < or > as the comparison operator the result is always false (again, because the integer as cast as a boolean, and true is neither greater nor less than true).

Some function to write out your own comparisson table in tsv format. Can be easily modified to add more testcases and/or binary functions. It will test all comparables against each other with all functions.

The truth tables really ought to be colorized; they're very hard to read as they are right now (just big arrays of TRUE and FALSE).

Also, something to consider: clustering the values which compare similarly (like is done on qntm.org/equality) would make the table easier to read as well. (This can be done simply by hand by rearranging the order of headings to bring related values closer together).

PHP's loose comparisons can be a huge convenience when used properly! It's extremely helpful to just remember the following are always FALSE:

null, false, "", 0, "0", array()

If your application never depends on a particular "empty/false/null/0/not set" value type, you won't have to worry about 99% of the other weird cases listed here. You won't need empty() or isset(). And ALL variable types will always work as expected for statements like:

(A) let "0" post values through like select or radio values that correspond to array keys or checkbox booleans that would return FALSE with empty(), and;
(B) screen out $x = "" values, that would return TRUE with isset()!

Because HTML forms post values as strings, this is a good way to test variables!

[[Editor Note: This will create a PHP Error of level E_NOTICE if the checked variable (in this case $_POST['var']) is undefined. It may be used after (in conjuection with) isset() to prevent this.]]