el表达式 for循环_在for循环中评估表达式

el表达式 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 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; }?>

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/evaluating-expressions-in-for-loops/

el表达式 for循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值