文字

PHP 类型比较表

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

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

Note:

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

Note:

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

使用 PHP 函数对变量 $x 进行比较
表达式 gettype() empty() is_null() isset() boolean : 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(); array TRUE FALSE TRUE FALSE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer FALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE
$x = -1; integer 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

松散比较 ==
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "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 TRUE TRUE
-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
array() FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
"php" TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE

严格比较 ===
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "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
array() 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

用户评论:

[#1] trexx68 [2015-04-25 17:11:30]

If you want to view the truth tables colorized, just:

1. Save a local copy of this page as an .html file,
2. View the page source with any text editor.
3. Replace the opening <head> tag so it will include this: 

<head> 
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

4. Replace the closing </body> tag with this: 

  <script>
    $( "td:contains('FALSE')" ).css("color", "red");
    $( "td:contains('TRUE')" ).css("color", "green");
  </script>
</body>

5. Save the file, and view it on your browser.  Enjoy.

[#2] aravind dot a dot padmanabhan at gmail dot com [2015-04-22 14:31:24]

please note that
 
$x = 0.0 ;
print empty($x); // returns 1 (true) 

$x = "0.0";
print empty($x); // returns blank (false);

[#3] Anonymous [2014-10-16 18:19:03]

The loose comparison chart is missing a few things.

array(1)==true returns true.

also:

(int)array(1) === 1 returns true

This is symmetric:

array(1) === (array)1 returns true

but the loose comparison

array(1)==1 still returns false.

[#4] blue dot hirano at gmail dot com [2014-09-07 02:29:39]

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).

[#5] lorenzo dot campagna at ymail dot com [2014-01-21 21:33:09]

Someone can explain why this comparison result TRUE  when it should be FALSE?

<?php

 $a 
"3abcdef";
 
$b 3;

 if(
$a == $b) {print("Match!");}    //Because of this comparison TRUE script will output:

?>

[#6] Anonymous [2013-11-21 13:43:02]

I'm running PHP 5.5.3.

This is a correction to one of the previous posts.

<?php
$o 
= new stdClass();
$o->we 12;

$o2 = new stdClass();
$o2->we 12;

$o3 = clone $o2;
var_dump($o == $o2); //true
var_dump($o === $o2); //false
var_dump($o3 === $o2); //false
?>


Output is:

true, false, false

[#7] crazy888s at hotmail dot com [2012-12-14 18:14:32]

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:

if(boolean && !string){...}
if(array){...}
if(!null || int){...}

Consider the same when working with your database values.

[#8] php at richardneill dot org [2012-04-12 19:45:28]

[Editor's note: As of PHP 5.4.4 this is no longer true. Integral strings that overflow into floating point numbers will no longer be considered equal.]

Be wary of string-comparison where both strings might be interpreted as numbers.  Eg:

$x="123456789012345678901234567890"; $y="123456789012345678900000000000";
echo  ($x==$y)?"equal":"not_equal";   #Prints equal !!

Both strings are getting converted to floats, then losing precision, then becoming equal :-(

Using "===" or making either of the strings non-numeric will prevent this.
[This is on a 32-bit machine, on a 64-bit, you will have to make the strings longer to see the effect]

[#9] Jouriy LYSENKO [2011-06-17 02:18:19]

If $var not declared.

In php 5.2 :
<?php if($var?>  - work

in php 5.3 :
<?php if($var?>  - dont work and generate error E_NOTICE

[#10] php at benizi dot com [2010-02-15 10:31:09]

It's interesting to note that 'empty()' and 'boolean : if($x)'
are paired as logical opposites, as are 'is_null()' and 'isset()'.

[#11] edgar at goodforall dot eu [2009-12-15 06:55:54]

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. 

<?php
$funcs 
= array(
        

        
'eq' => '==',
        
'ne' => '!=',
        
'gt' => '>',
        
'lt' => '<',
        
'ne2' => '<>',
        
'lte' => '<=',
        
'gte' => '>=',
        

        
'id' => '===',
        
'nid' => '!=='
);
class 
Test {
        protected 
$a;
        public 
$b;
        public function 
__construct($a,$b){
                
$this->$a;
                
$this->$b;
        }
        public function 
getab(){
                return 
$this->a.","$this->b;
        }

}
$tst1 = new Test(1,2);
$tst2 = new Test(1,2);
$tst3 = new Test(2,2);
$tst4 = new Test(1,1);

$arr1 = array(1,2,3);
$arr2 = array(2,3,4);
$arr3 = array('a','b','c','d');
$arr4 = array('a','b','c');
$arr5 = array();

$comp1 = array(
        
'ints' => array(-1,0,1,2),
        
'floats' => array(-1.1,0.0,1.1,2.0),
        
'string' => array('str''str1''''1'),
        
'bools' => array(truefalse),
        
'null' => array(null),
        
'objects' => array($tst1,$tst2,$tst3,$tst4),
        
'arrays' => array($arr1$arr2$arr3$arr4$arr5)
);
$fbody = array();

foreach(
$funcs as $name => $op){
        
$fbody[$name] = create_function('$a,$b''return $a ' $op ' $b;');
}

$table = array(array('function''comp1''comp2''f comp1 comp2''type'));

$comp2  = array();
foreach(
$comp1 as $type => $val){
        
$comp2[$type] = $val;
}

foreach(
$comp1 as $key1 => $val1){
        foreach(
$comp2 as $key2 => $val2){
                
addTableEntry($key1$key2$val1$val2);
        }
}
$out '';
foreach(
$table as $row){
        
$out .= sprintf("%-20s\t%-20s\t%-20s\t%-20s\t%-20s\n"$row[0], $row[1], $row[2], $row[3], $row[4]);
}

print 
$out;
exit;

function 
addTableEntry($n1$n2$comp1$comp2){
        global 
$table$fbody;
        foreach(
$fbody as $fname => $func){
                        foreach(
$comp1 as $val1){
  foreach(
$comp2 as $val2){
                                        
$val $func($val1,$val2);
                                                
$table[] = array($fnamegettype($val1) . ' => ' sprintval($val1), gettype($val2) .' => ' sprintval($val2), gettype($val) . ' => ' sprintval($val), gettype($val1) . "-" gettype($val2) . '-' $fname);
                                        }
                        }
        }
}

function 
sprintval($val){
        if(
is_object($val)){
                return 
'object-' $val->getab();
        }
        if(
is_array($val)){
                return 
implode(','$val);
        }
        if(
is_bool($val)){
                if(
$val){
                        return 
'true';
                }
                return 
'false';
        }
        return 
strval($val);
}

?>

[#12] info at shaelf dot ru [2008-01-06 13:51:14]

Compare object
<?php
$o 
= new stdClass();
$o->we 12;

$o2 = new stdClass();
$o2->we 12;

$o3 = clone $o2;
var_dump($o == $o2); //true
var_dump($o === $o2); //false
var_dump($o3 === $o2); //true
?>

[#13] frank [2007-08-14 15:06:35]

A comparison table for <=,<,=>,> would be nice...
Following are TRUE (tested PHP4&5):
NULL <= -1
NULL <= 0
NULL <= 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...).

[#14] Jan [2005-12-29 11:23:21]

Note that php comparison is not transitive:

"php" == 0 => true
0 == null => true
null == "php" => false

[#15] jerryschwartz at comfortable dot com [2005-07-26 13:04:32]

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).

[#16] tom [2005-06-17 02:27:52]

<?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.]]

[#17] aidan at php dot net [2005-01-24 07:00:06]

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.

上一篇: 下一篇: