PHP 比较操作符

来源:互联网 发布:python读取ods文件 编辑:程序博客网 时间:2024/05/27 06:56
Comparison OperatorsExampleNameResult$a == $bEqualTRUE if $a is equal to $b.$a === $bIdenticalTRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4) $a != $bNot equalTRUE if $a is not equal to $b.$a <> $bNot equalTRUE if $a is not equal to $b.$a !== $bNot identicalTRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4) $a < $bLess thanTRUE if $a is strictly less than $b.$a > $bGreater thanTRUE if $a is strictly greater than $b.$a <= $bLess than or equal to TRUE if $a is less than or equal to $b.$a >= $bGreater than or equal to TRUE if $a is greater than or equal to $b.

 

注意:如果将字符串和一个integer类型的值比较。string会转化为number类型(integer类型或者float类型),然后再和integer进行比较。

 

1 <?php
2  var_dump(0 == "a"); // 0 == 0 -> true
3  var_dump("1" == "01"); // 1 == 1 -> true
4 var_dump("1" == "1e0"); // 1 == 1 -> true
5
6 switch ("a") {
7 case 0:
8 echo "0";
9 break;
10 case "a": // never reached because "a" is already matched with 0
11 echo "a";
12 break;
13 }
14 ?>
15

 

 

Comparison with Various TypesType of Operand 1Type of Operand 2Resultnull or stringstringConvert NULL to "", numerical or lexical comparisonbool or nullanythingConvert to bool, FALSE < TRUEobjectobjectBuilt-in classes can define its own comparison, different classes are uncomparable, same class - compare properties the same way as arrays (PHP 4), PHP 5 has its own explanation string, resource or numberstring, resource or numberTranslate strings and resources to numbers, usual matharrayarrayArray with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value (see following example)arrayanythingarray is always greaterobjectanythingobject is always greater

 

在进行比较两个数的时候,最好先把这个表中的意思全部理解了。才好。

我曾经就在这个吃过亏。

 

还有就是,在比较两个value的时候,如果能使用 “===”和“!==”操作符的时候就使用这两个操作符。 以免php自作聪明的进行自动类型转换。

0 0