I wonder why this works strange. I understand that the difference is grouping, but does it matter in comparison?
$i = 0;
foreach ($items as $item) {
echo ($i == 0) ? 'first_row' : ($i == sizeof($feedbacks)-2) ? 'last_row' : 'none';
$i++;
}
returns
last_row
none
none
last_row
and
$i = 0;
foreach ($items as $item) {
echo ($i == 0) ? 'first_row' : (($i == sizeof($feedbacks)-2) ? 'last_row' : 'none');
$i++;
}
returns it correctly
first_row
none
none
last_row
Why is there a difference?
To use an explanation based on your code, a scaled down version would be:
In PHP, this is equivalent to writing:
On the first go,
$ihas the value of0, so the first ternary returns'first_row'and that string is used as the conditional for the second ternary — which in boolean context evaluates totrue— hence'last_row'is returned.If you regroup it:
then the result of the first ternary will not interfere with the second ternary.