Your right-handed ternary expression must be enclosed in parentheses, so it will be evaluated on its own as one expression:
$decimal_places = ($max <= 1) ? 2 : (($max > 3) ? 0 : 1); // Another way of looking at it $decimal_places = ($max <= 1) ? 2 : (($max > 3) ? 0 : 1);
Otherwise, your ternary expression evaluates from left to right, resulting in:
$decimal_places = (($max <= 1) ? 2 : ($max > 3)) ? 0 : 1; // Another way of looking at it $decimal_places = (($max <= 1) ? 2 : ($max > 3)) ? 0 : 1;
Which translates to if-else, becomes the following:
if ($max <= 1) $cond = 2; else $cond = ($max > 3); if ($cond) $decimal_places = 0; else $decimal_places = 1;
Therefore, $decimal_places ends as 0 for all values โโof $max except 2 , in which case it evaluates to 1 .
BoltClock Jan 26 '11 at 17:03 2011-01-26 17:03
source share