search for in the  
<Unix Domain: Unix and UDGList of Parser Tokens>
Last updated: Thu, 19 May 2005

Appendix O. 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.

Table O-1. Comparisons of $x with PHP functions

Expressiongettype()empty()is_null()isset()boolean : if($x)
$x = "";stringTRUEFALSETRUEFALSE
$x = NULLNULLTRUETRUEFALSEFALSE
var $x;NULLTRUETRUEFALSEFALSE
$x is undefinedNULLTRUETRUEFALSEFALSE
$x = array();arrayTRUEFALSETRUEFALSE
$x = false;booleanTRUEFALSETRUEFALSE
$x = true;booleanFALSEFALSETRUETRUE
$x = 1;integerFALSEFALSETRUETRUE
$x = 42;integerFALSEFALSETRUETRUE
$x = 0;integerTRUEFALSETRUEFALSE
$x = -1;integerFALSEFALSETRUETRUE
$x = "1";stringFALSEFALSETRUETRUE
$x = "0";stringTRUEFALSETRUEFALSE
$x = "-1";stringFALSEFALSETRUETRUE
$x = "php";stringFALSEFALSETRUETRUE
$x = "true";stringFALSEFALSETRUETRUE
$x = "false";stringFALSEFALSETRUETRUE

Table O-2. Loose comparisons with ==

 TRUEFALSE10-1"1""0""-1"NULLarray()"php"
TRUETRUEFALSETRUEFALSETRUETRUEFALSETRUEFALSEFALSETRUE
FALSEFALSETRUEFALSETRUEFALSEFALSETRUEFALSETRUETRUEFALSE
1TRUEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSE
0FALSETRUEFALSETRUEFALSEFALSETRUEFALSETRUEFALSETRUE
-1TRUEFALSEFALSEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSE
"1"TRUEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSE
"0"FALSETRUEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSEFALSE
"-1"TRUEFALSEFALSEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSE
NULLFALSETRUEFALSETRUEFALSEFALSEFALSEFALSETRUETRUEFALSE
array()FALSETRUEFALSEFALSEFALSEFALSEFALSEFALSETRUETRUEFALSE
"php"TRUEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSETRUE

Table O-3. Strict comparisons with ===

 TRUEFALSE10-1"1""0""-1"NULLarray()"php"
TRUETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
FALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
1FALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
0FALSEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
-1FALSEFALSEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSE
"1"FALSEFALSEFALSEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSE
"0"FALSEFALSEFALSEFALSEFALSEFALSETRUEFALSEFALSEFALSEFALSE
"-1"FALSEFALSEFALSEFALSEFALSEFALSEFALSETRUEFALSEFALSEFALSE
NULLFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSETRUEFALSEFALSE
array()FALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSETRUEFALSE
"php"FALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSETRUE

PHP 3.0 note: The string value "0" was considered non-empty in PHP 3, this behavior changed in PHP 4 where it's now seen as empty.



User Contributed Notes
PHP type comparison tables
Edgar dot Miller at gmail dot com
20-Apr-2005 06:55
Hi out there,

after not reading this part of the manual in past 5 years ;) ,  I found this great information about testing types for variables.

I could not stop until i wrote a method to get all information from the 'Tabelle O-1'.

so I put a number infront of every line of 'Tabelle O-1' and the method gives back the linenumber as result as it would be in 'Tabelle O-1'.

   define('TYPE_EMPTYSTRING',1);
   define('TYPE_UNDEFINED',2);
   define('TYPE_ARRAY',5);
   define('TYPE_BOOLEAN',6);
   define('TYPE_INTEGER',8);
   define('TYPE_INTEGER_ZERO',10);
   define('TYPE_STRING_INTEGER',12);
   define('TYPE_STRING_INTEGER_ZERO',13);
   define('TYPE_STRING',15);

 private function getTheType($var)
 {
 if ( gettype($var) == 'string' ) // 1 && 12 && 13 && 14 && 15 && 16 && 17
 {
 if ( is_null($var) === false && isset($var) === true )
 {

 if ( empty($var) === true && ((bool)($var)) === false ) // 1 && 13
 {
 if ( $var == '' )
 {
 $type = 1;
 }
 else
 {
 $type = 13;
 }

 }
 elseif ( is_numeric($var) === true) // 12 && 15
 {
 $type = 12; // && 14
 }
 else
 {
 $type = 15; // && 16 && 17
 }
 }
 else
 {
 $type = 99 ;
 }
 }
 elseif ( gettype($var) == 'integer' )
 {
 if ( empty($var) === false )
 {
 $type = 8; // && 9 && 11
 }
 else
 {
 $type = 10;
 }

 }
 elseif ( gettype($var) == 'boolean' )
 {
 $type = 6; // && 7
 }
 elseif ( gettype($var) == 'NULL' )
 {
 $type = 2; // && 3 && 4
 }
 elseif ( gettype($var) == 'array' )
 {
 $type = 5;
 }
 else
 {
 $type = 99;
 }

print "'".$var .' .. '. $type."'";
 return $type;
 }

now you can write small wrapper methods like this:

   function isNumberZero($var)
   {
       if ( getTheType($var) == TYPE_INTEGER_ZERO )
       {
           return true;
       }
       else
       {
           return false;
       }
   }

regards
Erdal

thanks to blueshoes and php for stating this information.
aidan at php dot net
24-Jan-2005 09:00
The way PHP handles comparisons when multiple types are concerned is quite confusing.

For example:
"php" == 0

This is true, because the string is casted interally to an integer. Any string (that does not start with a number), when casted to an integer, will be 0.

<Unix Domain: Unix and UDGList of Parser Tokens>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT