[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.
在前面的章节中,我们已经看过for
和foreach
循环。 在本章中,我们将看看另一种PHP编程语言环路while
和do 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 stepCODE
将在每个步骤中运行
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]