Php while循环中的备用div?
我需要在while循环中交替显示三个div。 第一个div的类名为box-hdr5,第二个div的类名为box-hdr4 有什么办法吗?请告诉我一些想法。Php while循环中的备用div?,php,html,while-loop,Php,Html,While Loop,我需要在while循环中交替显示三个div。 第一个div的类名为box-hdr5,第二个div的类名为box-hdr4 有什么办法吗?请告诉我一些想法。$i=0 $i = 0 while (true) { $class = ($i++ % 2 == 0) ? 'box-hdr5' : 'box-hdr4'; echo "<div class='" . $class . "'>stuff</div>"; } while(true){ $class=($i++%
$i=0
$i = 0
while (true) {
$class = ($i++ % 2 == 0) ? 'box-hdr5' : 'box-hdr4';
echo "<div class='" . $class . "'>stuff</div>";
}
while(true){
$class=($i++%2==0)?'box-hdr5':'box-hdr4';
呼应“东西”;
}
%是模运算符,返回整数除法的余数。当$i可被2整除时,余数将为0,这将每隔一次通过循环。您只需要有一个计数器,并计算出该数字是奇数还是偶数,这很容易用
你甚至不需要做2的模运算
$alternate = false;
while (/* true */) {
$alternate = !$alternate;
if ($alternate) {
// box-hdr4
} else {
// box-hdr5
}
}
但是上面的答案同样好。我想它应该是有效的,但在我的系统中不起作用。不知道怎么了…除了在第一行漏了一个分号,它还可以工作。“它不起作用”并不是你想要别人帮助你时所给出的那种解释,因为它告诉你有什么问题,而不是什么都不说。这只是一个实现目标的技术示例。你用它做了什么?什么不起作用?
$alternate = false;
while (/* true */) {
$alternate = !$alternate;
if ($alternate) {
// box-hdr4
} else {
// box-hdr5
}
}