Home > php教程 > php手册 > body text

PHP 的比较运算与逻辑运算 - jerrylsxu

WBOY
Release: 2016-05-20 10:13:19
Original
1140 people have browsed it

1、以下值用 empty() 被判断为true:

未赋值变量、未声明变量、0、"0"、""、false、null、空数组 array() 、对象的魔术方法 __get() 返回的值

在低于 PHP5.0 的版本中,没有任何属性的对象也被 empty 判断为 true

注意:empty() 只接受变量或变量的索引值或属性值,不能直接传入常量,也不能传入运算表达式,PHP 5.5 之后支持表达式

 

2、被 isset() 判断为 false 的值:未赋值变量、未声明变量、null、__get() 返回的值,接受的参与 empty()一样,不能是常量和表达式.

3、不同类型的数据比较

如果有一个是 boolean 型或者 null, 转换成 boolean 比较,

否则如果有一个是 number,转换成 number 比较

否则如果有一个是 string,转换成 string 比较

object 类型总是大于 array  类型和标量类型,array 类型总是大于 标量类型

 

注意这些比较结果:

复制代码
       <span style="color: #008000;">//</span><span style="color: #008000;">0开头的数字字符串转数字时不会按八进制转换,而是简单地丢弃把 '0' 丢弃按数字进行比较,</span>

      123=='0123' <span style="color: #008000;">//</span><span style="color: #008000;">true</span>
      "123"//<span style="color: #008000;">true,0开头的数字字符串直接按十进制数字比较而非八进制</span>

      "012" == 10 <span style="color: #008000;">//</span><span style="color: #008000;"> false</span>
       012 == 10  <span style="color: #008000;">//</span><span style="color: #008000;"> true</span>
       0x12 == 18  <span style="color: #008000;">//</span><span style="color: #008000;"> true</span>
       "0x12" == 18 <span style="color: #008000;">//</span><span style="color: #008000;"> true</span>
 

 <span style="color: #0000ff;">false</span> true; <span style="color: #008000;">//</span><span style="color: #008000;">true</span>
 2><span style="color: #0000ff;">true</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> false</span>
 2==<span style="color: #0000ff;">true</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> true </span>
 <span style="color: #0000ff;">null</span>==0; <span style="color: #008000;">//</span><span style="color: #008000;">true</span>
-1//<span style="color: #008000;">true</span>
-1null;<span style="color: #008000;">//</span><span style="color: #008000;">false ,-1 转 bool 是true</span>
Copy after login
复制代码

 

4、类型转换规则

 empty() 判断为 true 的值转换为 boolean 型得到 false ,反之,得到 true  ( __get() 返回的值需按具体的值判断)

被 empty() 判断为 true 的值转换成 number 得 0,非空的 array 转 number 得到1  ( __get() 返回的值需按具体的值判断)

复制代码
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Test{
   </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$k</span>=1<span style="color: #000000;">;
   </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __get(<span style="color: #800080;">$propertyName</span><span style="color: #000000;">){
       </span><span style="color: #0000ff;">return</span> 123<span style="color: #000000;">;
   }
}

</span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Test();
</span><span style="color: #0000ff;">echo</span> json_encode(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$obj</span>->k)); <span style="color: #008000;">//</span><span style="color: #008000;">true</span>
<span style="color: #0000ff;">echo</span> json_encode(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$obj</span>->k)); <span style="color: #008000;">//</span><span style="color: #008000;">false</span>
<span style="color: #0000ff;">echo</span> json_encode((bool)(<span style="color: #800080;">$obj</span>->k)); <span style="color: #008000;">//</span><span style="color: #008000;">true</span>
Copy after login
复制代码

 

几个string 转 number 的场景: 

复制代码
<span style="color: #0000ff;">echo</span> 'abc'*1 ; <span style="color: #008000;">//</span><span style="color: #008000;">0 </span>
<span style="color: #0000ff;">echo</span> '012'*1; <span style="color: #008000;">//</span><span style="color: #008000;">12  乘法:可以转换十六进制数,不是数字开头则返回 0</span>
<span style="color: #0000ff;">echo</span> '0x12.123'*1; <span style="color: #008000;">//</span><span style="color: #008000;">18</span>

<span style="color: #0000ff;">echo</span> (<span style="color: #0000ff;">float</span>)'0x12' ;<span style="color: #008000;">//</span><span style="color: #008000;">0 </span>
<span style="color: #0000ff;">echo</span> (int)'0x12' ; <span style="color: #008000;">//</span><span style="color: #008000;">0 不能处理十六进制数</span>
<span style="color: #0000ff;">echo</span> (<span style="color: #0000ff;">float</span>)'12abc'; <span style="color: #008000;">//</span><span style="color: #008000;">12 截取左侧字符串</span>
<span style="color: #0000ff;">echo</span> (<span style="color: #0000ff;">float</span>)'abc'; <span style="color: #008000;">//</span><span style="color: #008000;"> 0 不是数字返回0</span>

<span style="color: #008080;">is_numeric</span>('0x123'); <span style="color: #008000;">//</span><span style="color: #008000;">true 能识别十六进制数</span>
<span style="color: #008080;">is_numeric</span>('0x123.123'); <span style="color: #008000;">//</span><span style="color: #008000;">false 识别目标是整个字符串而非截取前面一部分</span>
Copy after login
复制代码

 

 

 

string 转 number 时截取左侧的数字字符串进行转换,如果没有则返回 0.

 

 

其他数据转字符串:

复制代码
<span style="color: #008000;">//几个转字符串的值</span>
Copy after login
(<span style="color: #0000ff;">string</span>)0 ; <span style="color: #008000;">//</span><span style="color: #008000;"> "0"</span>
(<span style="color: #0000ff;">string</span>)<span style="color: #0000ff;">true</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> "1"</span>
(<span style="color: #0000ff;">string</span>)<span style="color: #0000ff;">false</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> ""</span>
(<span style="color: #0000ff;">string</span>)<span style="color: #0000ff;">null</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> ""</span>
(<span style="color: #0000ff;">string</span>)<span style="color: #0000ff;">array</span>(); <span style="color: #008000;">//</span><span style="color: #008000;"> "<span>Array</span>"</span>
Copy after login
复制代码

数组可以直接进行字符串拼接操作但不能进行数学运算.

object 类型转换成 boolean 总是 true, object 类型不能转换成 number 和 string ,因此也不能进行字符串拼接和数学运算

标量转换成 array 的方式是将数组第一个元素设置成标量,返回该数组。

标量转换成 object 得到一个 stdClass 类的实例,标量的值被赋给名为 scalar 的属性: Object(    [scalar] => 234)

array 转 object 得到一个 stdClass 类的实例,数组的 key 为实力的属性名。

object 转 array 有点复杂:

           方法、静态属性、类常量被丢弃

           保护属性名称前面被被加上一个 "*"

           私有属性前面被加上类名作为前缀(大小写与类名完全相同)

    这些前缀的前后都加上空字符 \0

例如一个由 object 转换来的 array 为:

<span style="color: #0000ff;">Array</span>(    [*v] => 444    [bf] => 333    [bk] => 99977    [Ak] => 999    [*p] => 888    [a2] => 22)
Copy after login

 

原对象中有: 

           public 属性 a2, protected 属性 v、p ,这些属性来自哪个类无法鉴别(被重写则取子类的属性)

          来自类 b 的 private 属性 f、k,(从数组 key 来看,以bf为例,无法判断他是属性名为bf,还是来自类b的私有属性f)

          来自类 A 的 private 属性 k 

          无法鉴别 b 和 A 哪个是子类哪个是父类(仅从 array 的key来看,也无法推断出原对象构造自哪个类)

 

例子:

复制代码
<span style="color: #0000ff;">class</span><span style="color: #000000;"> A {
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$A</span> = 'private property, $A of class A'; <span style="color: #008000;">//</span><span style="color: #008000;"> This will become '\0A\0A'</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$C</span> = 'protected property, $C of class A'<span style="color: #000000;">;
}

</span><span style="color: #0000ff;">class</span> B <span style="color: #0000ff;">extends</span><span style="color: #000000;"> A {
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$A</span> = 'private property, $A of class B'; <span style="color: #008000;">//</span><span style="color: #008000;"> This will become '\0B\0A'</span>
    <span style="color: #0000ff;">public</span> <span style="color: #800080;">$AA</span> = 'public property, $AA of class B'; <span style="color: #008000;">//</span><span style="color: #008000;"> This will become 'AA'</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$B</span> = 'protected property, $B of class B'<span style="color: #000000;">;
}

</span><span style="color: #800080;">$arr</span> = (<span style="color: #0000ff;">array</span>) <span style="color: #0000ff;">new</span><span style="color: #000000;"> B();

</span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$arr</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {
    </span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$key</span> .',length: '.<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$key</span>).' value: '.<span style="color: #800080;">$value</span><span style="color: #000000;">;
}</span>
Copy after login
复制代码

 

输出结果:

BA,length: 4 value: <span style="color: #0000ff;">private</span> property, <span style="color: #800080;">$A</span> of <span style="color: #0000ff;">class</span><span style="color: #000000;"> B
AA</span>,length: 2 value: <span style="color: #0000ff;">public</span> property, <span style="color: #800080;">$AA</span> of <span style="color: #0000ff;">class</span><span style="color: #000000;"> B
</span>*B,length: 4 value: <span style="color: #0000ff;">protected</span> property, <span style="color: #800080;">$B</span> of <span style="color: #0000ff;">class</span><span style="color: #000000;"> B
AA</span>,length: 4 value: <span style="color: #0000ff;">private</span> property, <span style="color: #800080;">$A</span> of <span style="color: #0000ff;">class</span><span style="color: #000000;"> A
</span>*C,length: 4 value: <span style="color: #0000ff;">protected</span> property, <span style="color: #800080;">$C</span> of <span style="color: #0000ff;">class</span> A
Copy after login

 

 

5、 逻辑运算总是返回 true 或 false (写多了 javascript 的人要注意),逻辑运算符优先级从高到低 为 &&、 ||、 and、 or ,逻辑运算符的短路效果可以使用语句中,但记住他们不会像 javascript 中那样返回一个 不是 boolean 类型的值,在表达式中使用要注意。

复制代码
<span style="color: #800080;">$a</span> = 1<span style="color: #000000;">;
</span><span style="color: #800080;">$b</span>=0<span style="color: #000000;">;
</span><span style="color: #800080;">$b</span> and <span style="color: #800080;">$a</span> = 100<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">1</span>
<span style="color: #800080;">$b</span> || <span style="color: #800080;">$a</span> = 200<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">200</span>
Copy after login
复制代码

 

 

 

6、switch 的比较不是 "===" 而是 "==" (在 javascript 中是 "===")

 

7、 在 php4 中,object 之间的比较方式与 array 相同,在 php5 中 , object 类型间的 "==" 比较为 true的前 提是,他们属于同一个类的实例(当然还要进行属性的比较,这类似标量的"==="比较),object 间的 "===" 比较为 true 的前提是他 们 就是同一个对象。

 

在 PHP4 中 ,不包括任何成员变量的对象 被 empty() 判断为 true

字符串偏移 offset 取字符的 empty() 判定: 取对应 offset 的字符进行判断,在PHP5.4 以前,使用索引从字符串中取字符时会先将索引进行取整,因此左侧不包含数字的字符串都被转换成0,PHP5.4之后,不再对非整形格式的字符串索引进行取整,因此判断为 true, 同理,isset() 判定为false. 如:

复制代码
<span style="color: #800080;">$str</span> = 'ab0d'<span style="color: #000000;">;
</span><span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$str</span>[0]); <span style="color: #008000;">//</span><span style="color: #008000;">false</span>
<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$str</span>[0.5]); <span style="color: #008000;">//</span><span style="color: #008000;">false  索引被向下取整 为 0</span>
<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$str</span>["0.5"]); <span style="color: #008000;">//</span><span style="color: #008000;">false 索引被向下取整 为 0,PHP5.4之后不取证,判定为 true </span>
<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$str</span>[2]); <span style="color: #008000;">//</span><span style="color: #008000;">true ,取得的字符为 "0"</span>
<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$str</span>["3"]); <span style="color: #008000;">//</span><span style="color: #008000;">false ,取得的字符为 "d"</span>
<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$str</span>[4]); <span style="color: #008000;">//</span><span style="color: #008000;">true ,索引超出范围,notice 警告,但 empty() 会忽略警告</span>
<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$str</span>['a']); <span style="color: #008000;">//</span><span style="color: #008000;"> false ,左侧不包含数字字符串索引 PHP5.4之前被处理为 $str[0],PHP5.4之后,直接为判定 true </span>
Copy after login
复制代码

 

无论是“不等于”还是”==“ ,不要在 PHP 的跨类型数据比较中使用”传递性“:

$a == $b; //true

$b == $c; //true

并不能说明 $a == $c 为 true

 

数组的比较方法

复制代码
<span style="color: #008000;">//</span><span style="color: #008000;"> 数组是用标准比较运算符这样比较的</span>
<span style="color: #0000ff;">function</span> standard_array_compare(<span style="color: #800080;">$op1</span>, <span style="color: #800080;">$op2</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">count</span>(<span style="color: #800080;">$op1</span>) count(<span style="color: #800080;">$op2</span><span style="color: #000000;">)) {
        </span><span style="color: #0000ff;">return</span> -1; <span style="color: #008000;">//</span><span style="color: #008000;"> $op1 
    } <span style="color: #0000ff;">elseif</span> (<span style="color: #008080;">count</span>(<span style="color: #800080;">$op1</span>) > <span style="color: #008080;">count</span>(<span style="color: #800080;">$op2</span><span style="color: #000000;">)) {
        </span><span style="color: #0000ff;">return</span> 1; <span style="color: #008000;">//</span><span style="color: #008000;"> $op1 > $op2</span>
<span style="color: #000000;">    }
    </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$op1</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$val</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">array_key_exists</span>(<span style="color: #800080;">$key</span>, <span style="color: #800080;">$op2</span><span style="color: #000000;">)) {
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> uncomparable</span>
        } <span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$val</span> $op2</span>[<span style="color: #800080;">$key</span><span style="color: #000000;">]) {
            </span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">;
        } </span><span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$val</span> > <span style="color: #800080;">$op2</span>[<span style="color: #800080;">$key</span><span style="color: #000000;">]) {
            </span><span style="color: #0000ff;">return</span> 1<span style="color: #000000;">;
        }
    }
    </span><span style="color: #0000ff;">return</span> 0; <span style="color: #008000;">//</span><span style="color: #008000;"> $op1 == $op2</span>
}
Copy after login
复制代码

 

 

 

8、三元运算符 ?: ,跟其他大多数编程语言不一样,PHP 的三元运算符是左结合的!

复制代码
    <span style="color: #800080;">$arg</span> = 'T'<span style="color: #000000;">;    
    </span><span style="color: #800080;">$vehicle</span> = ( ( <span style="color: #800080;">$arg</span> == 'B' ) ? 'bus' :<span style="color: #000000;">    
                 ( </span><span style="color: #800080;">$arg</span> == 'A' ) ? 'airplane' :<span style="color: #000000;">   
                 ( </span><span style="color: #800080;">$arg</span> == 'T' ) ? 'train' :<span style="color: #000000;">    
                 ( </span><span style="color: #800080;">$arg</span> == 'C' ) ? 'car' :<span style="color: #000000;">    
                 ( </span><span style="color: #800080;">$arg</span> == 'H' ) ? 'horse' :    
                 'feet'<span style="color: #000000;"> );    
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$vehicle</span>;   <span style="color: #008000;">//</span><span style="color: #008000;">horse</span>
Copy after login
复制代码


 

三元运算表达式被划分为

( <span style="color: #800080;">$arg</span> == 'B' ) ? 'bus' : ( <span style="color: #800080;">$arg</span> == 'A'<span style="color: #000000;"> ) 
                                    </span>? 'airplane' : ( <span style="color: #800080;">$arg</span> == 'T'<span style="color: #000000;"> ) 
                                                             </span>? 'train' : ( <span style="color: #800080;">$arg</span> == 'C'<span style="color: #000000;"> )
                                                                               </span>? 'car' : ( <span style="color: #800080;">$arg</span> == 'H'<span style="color: #000000;"> )
                                                                                                    </span>? 'horse' : 'feet' ;   
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template