附录 O. PHP 类型比较表

以下的表格显示了 PHP 类型比较运算符在松散和严格比较时的作用。该补充材料还和类型戏法的相关章节内容有关。同时,大量的用户注释和 BlueShoes 的工作也给该材料提供了帮助。

在使用这些表格之前,需要明白变量类型及它们的意义。例如,"42" 是一个字符串而 42 是一个整数。FALSE 是一个布尔值而 "false" 是一个字符串。

注: HTML 表单并不传递整数、浮点数或者布尔值,它们只传递字符串。要想检测一个字符串是不是数字,可以使用 is_numeric() 函数。

注: 在没有定义变量 $x 的时候,诸如 if ($x) 的用法会导致一个 E_NOTICE 级别的错误。所以,可以考虑用 empty() 或者 isset() 函数来初始化变量。

表格 O-1. 用 PHP 函数对 $x 的比较

表达式gettype()empty()is_null()isset()boolean : if($x)
$x = "";stringTRUEFALSETRUEFALSE
$x = NULLNULLTRUETRUEFALSEFALSE
var $x;NULLTRUETRUEFALSEFALSE
$x 尚未定义NULLTRUETRUEFALSEFALSE
$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

表格 O-2. 用 == 进行松散比较

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

表格 O-3. 用 === 进行严格比较

 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 注意事项: 在 PHP 3 中,字符串 "0" 被认为是非空的,这个情况在 PHP 4 中发生了改变,它将被认为是空值。


add a note add a note User Contributed Notes
omit
24-Aug-2006 02:32
the manual said "HTML Forms do not pass integers, floats, or booleans; they pass strings"

while this is true, php will sometimes change the type to either type array, or possibly type integer(no, not a numeric string) if it was used as an array key. php seems to do this when it parses the request data into the predefined variable arrays.

example:

<input type="text" name="foo[5]">
<input type="text" name="foo[7]">

now obviously the browser will send those names as a string. but php will change thier type.

<?php

// $_POST['foo'] is an array
var_dump($_POST['foo']);

foreach (
$_POST['foo'] as $key => $val) {
  
// the keys 5 and 7 will be type integer
  
var_dump($key);
}

?>

because of this, its also a good idea to check the types of your variables.
Jan
30-Dec-2005 03:23
Note that php comparison is not transitive:

"php" == 0 => true
0 == null => true
null == "php" => false
php [at] barryhunter [.] co [.] uk
08-Sep-2005 03:44
In case it helps someone, here's a table to compare different Variable tests (created before I found this page!)

http://www.deformedweb.co.uk/php_variable_tests.php
jerryschwartz at comfortable dot com
27-Jul-2005 04:04
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).
tom
17-Jun-2005 05:27
<?php
if (strlen($_POST['var']) > 0) {
  
// form value is ok
}
?>

When working with HTML forms this a good way to:

(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.]]
aidan at php dot net
24-Jan-2005 11: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.