Web Development PHP Programming - Learn PHP
 Developer Newsletter

Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML


Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn Pear
Free White Papers



Scripts Directory
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET Hosting
Budget Web Hosting
Dedicated Servers
Linux Web Hosting
Shared Hosting
Small Business Hosting
Windows Web Hosting

Developer Resources
Developer Tools
Developer News
Developer Content
Developer Book Reviews
Survey Software




Appendix R. 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 R-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 R-2. Loose comparisons with ==

 TRUEFALSE10-1"1""0""-1"NULLarray()"php"""
TRUETRUEFALSETRUEFALSETRUETRUEFALSETRUEFALSEFALSETRUEFALSE
FALSEFALSETRUEFALSETRUEFALSEFALSETRUEFALSETRUETRUEFALSETRUE
1TRUEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSE
0FALSETRUEFALSETRUEFALSEFALSETRUEFALSETRUEFALSETRUETRUE
-1TRUEFALSEFALSEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSEFALSE
"1"TRUEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSE
"0"FALSETRUEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSE
"-1"TRUEFALSEFALSEFALSETRUEFALSEFALSETRUEFALSEFALSEFALSEFALSE
NULLFALSETRUEFALSETRUEFALSEFALSEFALSEFALSETRUETRUEFALSETRUE
array()FALSETRUEFALSEFALSEFALSEFALSEFALSEFALSETRUETRUEFALSEFALSE
"php"TRUEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSETRUEFALSE
""FALSETRUEFALSETRUEFALSEFALSEFALSEFALSETRUEFALSEFALSETRUE

Table R-3. Strict comparisons with ===

 TRUEFALSE10-1"1""0""-1"NULLarray()"php"""
TRUETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
FALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
1FALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
0FALSEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
-1FALSEFALSEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
"1"FALSEFALSEFALSEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSEFALSE
"0"FALSEFALSEFALSEFALSEFALSEFALSETRUEFALSEFALSEFALSEFALSEFALSE
"-1"FALSEFALSEFALSEFALSEFALSEFALSEFALSETRUEFALSEFALSEFALSEFALSE
NULLFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSETRUEFALSEFALSEFALSE
array()FALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSETRUEFALSEFALSE
"php"FALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSETRUEFALSE
""FALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSETRUE

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.







About the NetVisits, Inc Network | Advertise
Developer Tutorials hosted by HostGator.
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: