php中的多条件与赋值

php中的多条件与赋值,php,Php,关于php如何运行的一个简单问题: getColor是Circle类的一个函数,它返回false或颜色作为属性的对象。如果我这样做: $res=$circle->getColor(); if ($res && $res->color=='white') { echo "ok"; } else { echo "no"; } 我得到“ok”,但如果我得到了 if ($res=$circle->getColor() && $res

关于php如何运行的一个简单问题:

getColor是Circle类的一个函数,它返回false或颜色作为属性的对象。如果我这样做:

$res=$circle->getColor();
if ($res && $res->color=='white')
{
    echo "ok";
 } else {
    echo "no";
 }
我得到“ok”,但如果我得到了

if ($res=$circle->getColor() && $res->color=='white')
{
    echo "ok";
 } else {
    echo "no";
 }  
我得到“不”。为什么?我以为第一个条件是先执行的。不是吗?

因为。因为
&&
=
具有更高的优先级,PHP可以有效地看到这一点:

if ($res = ($circle->getColor() && $res->color=='white'))
为了获得所需的行为,应将第一个条件括起来:

if (($res = $circle->getColor()) && $res->color == 'white')

你真的不应该这样做,因为你的逻辑很混乱。然而,我很好奇为什么你会看到你所看到的行为。有些东西告诉我解析器误解了。试着用括号更明确一点。谢谢。我把它放在书签里了,因为我从来不记得这些东西。我犯了括号的错误。