我有类似的东西
只是一个片段
$i = 1; while (...) {
echo ($i % 5 == 1) ? 'class="first-col"' : ($i % 5 == 0) ? 'class="last-col"' : '';
$i++;
}
但即使 $i % 5 == 1
,我也会得到 class="last-col"
echo-ed 我的逻辑对吗?
这实际上是 IE 的 CSS 修复,所以我不需要使用 nth-child
。我正在尝试定位包含 5 列/行的网格的第一列和最后一列
最佳答案
?:
运算符是左结合的,即你有
echo ( ($i % 5 == 1) ? 'class="first-col"' : ($i % 5 == 0) ) ? 'class="last-col"' : '';
参见 http://php.net/manual/en/language.operators.comparison.php
最好不要嵌套三元运算符。
最好使用 if
/elseif
/else
结构,它们更易读。
关于php - 为什么我的条件回显是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5579067/