关于PHP语法中的 or 与 || 的问题。

查看:146
本文介绍了关于PHP语法中的 or 与 || 的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

    $a = 0;    
    $b = 0;    
    
    if($a=3 or $b=3){
        $a++;
        $b++;
    }
    echo $a.",".$b;

返还值为4,1

    $a = 0;    
    $b = 0;    
    
    if($a=3 || $b=3){
        $a++;
        $b++;
    }
    echo $a.",".$b;

返还值为1,1

why?
第一则中的or语法错了嘛?
wamp环境,php5.5.12

解决方案

1.首先,请查看php.net对所有运算符优先级顺序规定表:http://php.net/manual/zh/language.operators.precedence.php
2.我们发现||大于=大于or(这里指运算符优先级),且=是右结合顺序;
3.因此,在第二段snippet中的if条件结合顺序应该是if($a=(3||($b=3))),因为php.net上面的链接文档又说了'Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation. PHP does not (in the general case) specify in which order an expression is evaluated and code that assumes a specific order of evaluation should be avoided, because the behavior can change between versions of PHP or depending on the surrounding code.',翻译一下就是说:运算符的优先级和结合性只是决定了表达式如何分组,却没有指定代码被如何解析执行。。。,所以这里的if条件并不能完全按照运算符的优先级和结合性来判断代码如何解析执行。

关注PHP技术大全微信公众号(phpgod),拿起手机,打开微信,轻松一扫下面的二维码,每天成长一点,成就大神就不远。

这篇关于关于PHP语法中的 or 与 || 的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆