An easy question about how php behaves:
getColor is a function of class Circle which returns either false or an object with the color as an attribute. If I do:
$res=$circle->getColor();
if ($res && $res->color=='white')
{
echo "ok";
} else {
echo "no";
}
I get “ok”, but if I do
if ($res=$circle->getColor() && $res->color=='white')
{
echo "ok";
} else {
echo "no";
}
I get “no”. Why? I thought first condition is executed first. Isn’t it?
Because of operator precedence. Because
&&has higher precedence than=PHP is effective seeing this:In order to get the behavior you want, you should parenthesize the first condition: