本文介绍了PHP for循环剩余值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有没有更多的专业或更简单的方法来实现一个PHP的循环,这也迭代时,只有剩余的元素? (在当前例子中:1126) 示例eval.in/46075rel =nofollow>演示: $ p $ for($ i = 0;( $ i + = $ step)< $ max;){ echo $ i,','; } echo $ max; 节目输出: 2000,4000,6000,8000,10000,11126 只是一些年龄较大的玩... ... 要创建您的输出(最后用逗号)使用不同类型的循环,循环后检查循环: 示例/ 演示: $ i = 0; do { $ i + = $ step; echo min($ i,$ max),','; } while($ i 节目输出: 2000,4000,6000,8000,10000,11126, 或者为什么不是 for 循环与输出在前提条件? 演示: $ i = 0; printf('%d,',min($ i + = $ step,$ max))&&( $ i ); 节目输出: 2000,4000,6000,8000,10000,11126, Is there any more professional or easier way to achieve a PHP for loop, that also iterates when there are only remaining elements? (In the current example: 1126)<?php$max = 11126;$step = 2000;for ($i = 0; $i < $max; null) { if ($max - $i > $step) { $i += $step; } else { $i += $max - $i; } echo($i . ", ");}?>Outpts:2000, 4000, 6000, 8000, 10000, 11126, ...which is correct, but looks like too much of code. 解决方案 Well, as the loop already has the continue decision, there is always something to output because $max is already the exit:Example/Demo:for ($i = 0; ($i += $step) < $max;) { echo $i, ', ';}echo $max;Program Output:2000, 4000, 6000, 8000, 10000, 11126All what follows is just some older playing around... .To create exactly your output (with the comma at the end) you can use a different kind of loop that checks to loop after it looped:Example/Demo:$i = 0;do { $i += $step; echo min($i, $max), ', ';} while ($i < $max);Program Output:2000, 4000, 6000, 8000, 10000, 11126, Or why not a for loop with having the output in the pre-condition?Example/Demo:for ( $i = 0; printf('%d, ', min($i += $step, $max)) && ($i < $max););Program Output:2000, 4000, 6000, 8000, 10000, 11126, 这篇关于PHP for循环剩余值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 18:47