0

in php

$var =  true ? '1' : false ? '2' : false ? '3' : '4';
echo $var;

output is 3

in Java

char cond =  true ? '1' : false ? '2' : false ? '3' : '4';
System.out.println( cond );

output is 1

I completely understand how Java performed the logic. but i can't get how php will output 3. need help to understand how php actually evaluated that.

Jonathon Reinhart
  • 124,861
  • 31
  • 240
  • 314
Loon Yew
  • 51
  • 1
  • 6

1 Answers1

0

Precedence with parentheses

$var =  ((true ? '1' : false) ? '2' : false) ? '3' : '4';
echo $var;

Documentation: http://php.net/language.operators.comparison#example-138

Some explanation: https://bugs.php.net/bug.php?id=61915

sectus
  • 15,278
  • 5
  • 51
  • 91