Php – While和Do While循环

[rps-include post=6522]

[rps-include post = 6522]

In previous chapter we have looked for and foreach loops. In this chapter we will look another Php programming language loop while and do while loops.

在前面的章节中,我们已经看过forforeach循环。 在本章中,我们将看看另一种PHP编程语言环路whiledo while循环。

While循环 (While Loop)

while loop iterates over given code block unless the condition met. The condition is checked in before every step. If the condition do not meet the while loop will end. Syntax is like below.

除非满足条件,否则while循环在给定的代码块上迭代。 在每一步之前都要检查条件。 如果条件不满足,while循环将结束。 语法如下。

while(CONDITION){
CODE
}
  • CONDITION is checked after every step

    每一步后检查CONDITION

  • CODE will run in every step

    CODE将在每个步骤中运行

In this example we will count from 1 to 10 and use variable named $counter and increase it in every step.

在此示例中,我们将从1计数到10并使用名为$counter变量,并在每个步骤中将其增加。

$counter = 1;

while($counter<=10){

    echo $counter;

    $counter++;

}

If we run code above we will get following output. The name of the code file is index3.php

如果我们在上面运行代码,将得到以下输出。 该代码文件的名称为index3.php

$ php index3.php

循环执行 (Do While Loop)

Do while is very same with while but the sequence is reverse. First the CODE block is executed and then the CONDITION is checked. Syntax is like below.

虽然while与while相同,但是顺序相反。 首先执行CODE块,然后检查CONDITION 。 语法如下。

do{

CODE

} while (CONDITION)

In this example we will do same of previous example.

在此示例中,我们将与前面的示例相同。

$counter = 1; 
 
do{ 
    echo $counter."\n"; 
 
    $counter++; 
 
}while($counter<=10)

[rps-include post=6522]

[rps-include post = 6522]

LEARN MORE  Powershell Comparison Operators Like Equal, Greater, Lesser, Contains, Regex
了解更多Powershell比较运算符,例如Equal,Greater,Lesser,Contains,Regex

翻译自: https://www.poftut.com/php-while-and-do-while-loops/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值