看这张表:
<?php
echo '<table>';
for($i = 1;$i<99;$i++)
{
$i = intval($i);
if ($i / 3 == intval($i / 3))
{
echo "<tr><td>this is third tr</td></tr>";
}
else
{
echo "<tr><td>this is first or second</td></tr>";
}
}
输出类似于
this is first or second
this is first or second
this is third tr
this is first or second
this is first or second
this is third tr
this is first or second
this is first or second
this is third tr
是否有更优雅的方法来检测ALWAYS第三tr?
您是否在寻找类似:
<?php
echo '<table>';
for($i = 1;$i<99;$i++){
if ($i % 3 == 0){
echo "<tr><td>this is third tr</td></tr>";
}else{
echo "<tr><td>this is first or second</td></tr>";
};
};
echo "</table>";
?>
http://php.net/manual/en/language.operators.arithmetic.php 使用模数运算符…
if ( ! (i % 3))
返回除法的余数。因为优先级顺序,所以需要括号
CodePad .
$i = intval($i)
也是冗余的。$i
已经是一个整数