php - Operators precedence of "or" and assignment -


found interesting code snippet today. simplified, looks this:

$var = null;  $var or $var = '123';  $var or $var = '312';  var_dump($var); 

the thing that, know, precedence of assignment higher or, so, assume, var_dump should output 312 (first - assign, second - compare logically). result defferent, getting 123 (first - check if $var converting true, second - if not, assign value).

the questions how work?

why behavior same or , ||?

you can see examples behaviour in logical operators

also can read artical short-circuit evaluation

the short-circuit expression x sand y (using sand denote short-circuit variety) equivalent conditional expression if x y else false; expression x sor y equivalent if x true else y.

in php.

return x() , y(); 

equal to

if (x())   return (bool)y(); else   return false; 

return x() or y(); 

equal to

if (x())   return true; else   return (bool)y(); 

so, deal not in precedence.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -