Search code examples
phpoperator-keywordoperator-precedence

Operator precedence in php


We know = is lower precedence than !.

My Question: if the above sentences is true then how to execute the following if() condition

function foo()
{ 
   return false;
}


if(!$a=foo())
{
  echo "Yes, foo() appeared here.";
}

Solution

  • This is an assignment, not a comparison. Besides, you have a function call which is needed to the assignment. Then the order is:

    1) Function call returning false;
    2) Assignment of false value to $a;
    3) Negation of $a as !false, i.e., true.