$i = 2;
$result = ($i == 2) ? "Two" : ($i == 1) ? "One" : "Other";
echo $result; // outputs: One
While the same code in C# outputs: Two
int i=2;
String result = (i == 2) ? "Two" : (i == 1) ? "One" : "Other" ;
Console.Write( result ); // outputs: Two
Ternary operators are evaluated LEFT-TO-RIGHT.
($i == 2) ? "Two" : ($i == 1) ? "One" : "Other"
"Two" ? "One" : "Other" // first part evaluated to "Two"
"One" // non-empty strings evaluate to true
So you should get One in your output, not Other. It's a little tricky.
Wise words from the manual:
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With