[英]how can i manipulate the end output of a While Loop in PHP?
I want to manipulate the end output of a while loop . 我想操纵while循环的结束输出 。
In this example, i am using number 5 in place of a variable. 在此示例中,我使用数字5代替变量。
<?php $i=1; while($i<=5) {
echo "col-md-" . round(12/5) . "<br>";
$i++;
} ?>
giving output of 输出
col-md-2 ---> I want to show 4 here by applying the below math >> col-md-2 --->我想通过应用以下数学方式在这里显示4 >>
12-(5-1)*round(12/5) 12-(5-1)*回合(12/5)
Can you please help me with this. 你能帮我这个忙吗? is it possible? 可能吗? A help will be very much appreciated. 非常感谢您的帮助。
One option could be to check if the variable $i
equals 5 before it gets incremented: 一种选择是在变量$i
递增之前检查变量$i
等于5:
$i=1;
while($i<=5) {
echo "col-md-" . round(12/5) . "<br>";
if ($i === 5) {
echo "col-md-" . (12-(5-1)*round(12/5));
}
$i++;
}
If it is always at the end, you can just add it after the while loop: 如果始终在末尾,则可以在while循环之后添加它:
echo "col-md-" . (12-(5-1)*round(12/5));
An alternative is to use a for()
loop and end it one earlier and then output an extra column after... 一种替代方法是使用for()
循环并提前结束它,然后在之后输出额外的列...
$count = 5; // Number of columns
for ( $i = 1; $i < $count; $i++ ) {
echo "col-md-" . round(12/$count) . "<br>";
}
echo "col-md-" . (12-($count-1)*round(12/$count)) . "<br>";
Which gives... 这使...
col-md-2<br>col-md-2<br>col-md-2<br>col-md-2<br>col-md-4<br>
This is the final code, which solved my issue. 这是最终的代码,解决了我的问题。
$count = 1; // Number of columns
for ( $i = 1; $i < $count; $i++ ) {
echo "col-md-" . floor(12/$count) . "<br>";
}
if (($count == 5 || 7 || 8 || 9 || 10 || 11)) {
echo "col-md-" . (12-($count-1)*floor(12/$count)) . "<br>";
}
Thanks to @nigel and @thefourthbird. 感谢@nigel和@thefourthbird。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.