Home > Net >  Weird behavior of coalescing operator (??) in php
Weird behavior of coalescing operator (??) in php

Time:12-24

Can someone please explain, why the results are different in below two cases?

<?php

echo ((7)??0 / (15)??1);

echo "<br><br>";

echo (((7)??0) / ((15)??1));
?>

CodePudding user response:

See Operator Precedence in the PHP manual.

?? has higher precendance than / so in the first example since 7 is a true value it wins.

In the second example, the parentheses force precedence.

  •  Tags:  
  • php
  • Related