el表达式for循环_在for循环中评估表达式
el表达式 for循环
The usual syntax of a for-loop is < ?php for($i = 0; $i < 10; $i++){ // do your thing } ?>
for循环的常⽤语法是<?php for($ i = 0; $ i <10; $ i ++){//做你的事}?>
where
哪⾥
1. $i = 0 is evaluated once before the loop starts
$i = 0在循环开始前被评估⼀次
2. $i < 10 is the condition of the loop, it's executed the number of times the loop executes plus 1. Why plus 1? Well the
plus 1 is just to check we're at the end of the loop and no more looping is needed. In this example, this expression will be evaluated 11 times
$i < 10是循环的条件,它的执⾏次数是循环执⾏的次数加1。为什么加1? 好吧,加号1只是⽤来检查我们是否处在循环的末尾,⽽不再需要循环。 在此⽰例中,此表达式将被求值11次
3. $i++ is an expression that executes after everything in the brackets is executed, i.e. at the end of each iteration. So in
our example it's executed 10 times
$i++是⼀个表达式,它在括号内的所有内容执⾏完后即每次迭代结束时执⾏。 因此,在我们的⽰例中,它执⾏了10次
The three expressions described above can be also function calls or something else, they are not necessarily always used as in the example above. Consider the next example as an illustration of how other functions can be called. All these functions print a line, so executing this will give you a visual idea what is executed, when and how many times. < ?php function start(){ echo 'start'; return 0; } function condition($i){ echo 'condition: ' . $i; return 10; } function increment(&$i){ echo 'increment: ' . $i; $i++; } for ($i = start(); $i < condition($i); increment($i)){ // do nothing } ?> The loop in this example is doing exactly the same as the first loop, only it's defined fancier, using functions and all the three functions print a line. Here's what the
el表达式获取值output of the script would be: start condition: 0 increment: 0 condition: 1 increment: 1 condition: 2 increment: 2 condition: 3 increment: 3 condition: 4 increment: 4 condition: 5 increment: 5 condition: 6 increment: 6 condition: 7 increment: 7 condition: 8 increment: 8 condition: 9 increment: 9 condition: 10
上⾯描述的三个表达式也可以是函数调⽤或其他函数,它们不⼀定像上⾯的⽰例中那样始终使⽤。 考虑下⼀个⽰例,以说明如何调⽤其他函数。 所有这些功能都会打印⼀条线,因此执⾏此操作将使您直观地知道执⾏的内容,执⾏的时间和次数。 <?php函数start()
{echo'start'; 返回0; }函数condition($ i){echo'condition:'。 $ i; 返回10; }函数crement(&$ i){echo'increment:'。 $ i; $ i ++; }
for($ i = start(); $ i <condition($ i); increment($ i)){//不做任何动作}?>此⽰例中的循环与第⼀个循环完全相同,只是定义的发烧友,使⽤函数和所有三个函数打印⼀⾏。 脚本的输出如下所⽰: start condition: 0 increment: 0 condition: 1 increment: 1 condition: 2 increment: 2 condition: 3 increment: 3 condition: 4 increment: 4 condition: 5 increment: 5 condition: 6 increment: 6 condition: 7 increment: 7 condition: 8 increment: 8 condition: 9 increment: 9 condition: 10
So just a word of caution - in the conditions, be careful not to you use potentially slow functions or expressions that return the same value every time they're exeuted, as they are going to be needlessly executed the loop count plus 1 times.
因此,请注意-在这种情况下,请注意不要使⽤可能会变慢的函数或表达式,它们每次执⾏时都返回相同的值,因为它们将不必要地执⾏循环计数加1次。
As a quick example: < ?php // not good for ($i = 0; $i < 100*99%98+189*345/2*56-1; $i++) { echo $i; } // better $boundary = 100*99%98+189*345/2*56-1; for ($i = 0; $i < $boundary; $i++) { echo $i; } ?>
举个简单的例⼦:<?php //不适合($ i = 0; $ i <100 * 99%98 + 189 * 345/2 * 56-1; $ i ++){echo $ i; } //更好的$ boundary = 100 * 99%98 + 189 * 345/2 * 56-1; 对于($ i = 0; $ i <$ boundary; $ i ++){echo $ i; }?>
el表达式 for循环
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论