本人新手一个,看到一段很实用的代码,和大家分享一下,还请各位多多指教。
需求:php循环输出列表 隔两行一换颜色,效果如下
分析:我们需要 1,2 行黑色 3,4灰色 以此类推,仔细观察 3,4 7,8 11,12 ... 这些数字无论减去1或者2都无法被4整除,那么我们可以这么写。
代码:
<?php
for ($i=1; $i <25 ; $i++) {
$a=$i-1;
$b=$i-2;
if($a%4==0||$b%4==0){
echo ' <div style="background-color: #555;margin:5px;">'.$i.'</div>';
}else{
echo ' <div style="background-color: #999;margin:5px;">'.$i.'</div>';
}
}
注意:注意变量i的开始位置不要为0