如何避免在循环的最后一次迭代中回显一行?

How to avoid echoing a line on that last iteration of a loop?

如何阻止分页符在最后一次迭代时回显?

for($k=0; $k < count($selectedmonth); $k++){

    // some other code

    echo "<br pagebreak='true'>";

}
$array_length = count($selectedmonth);
for($k = 0; $k < $array_length; $k++){
    if($k != $array_length - 1){
        echo '<br pagebreak="true">';
    }
}

将计数声明为变量并有条件地检查它。通过将其设置为变量,可以减少函数调用。

for($k=0,$count=count($selectedmonth);$k<$count;++$k) {
    if($count!=$k+1){echo "<br pagebreak='true'>";}
}

如何从循环中排除最后一个元素?

 for($k=0;$k<count($selectedmonth)-1;$k++) {

    echo "<br pagebreak='true'>";

    }

您可以使用以下代码来完成,而无需每次都使用 count()if()

$end = count($selectedmonth) - 1;
for($k = 0; $k < $end; $k++) {
    echo "<br pagebreak='true'>";
}