Php 简明教程

PHP - While Loop

在 PHP 脚本中创建循环的最简单的办法是使用 while 结构。PHP 中 while 循环的语法与 C 语言中的类似。只要 while 语句中的布尔表达式为 true,循环体块就会被反复执行。

The easiest way to create a loop in a PHP script is with the while construct. The syntax of while loop in PHP is similar to that in C language. The loop body block will be repeatedly executed as long as the Boolean expression in the while statement is true.

以下流程图有助于理解 PHP 中 while 循环如何运行 −

The following flowchart helps in understanding how the while loop in PHP works −

php while loop

表达式中的值在每次循环开始之前都会被检查。如果 while 表达式从一开始就评估为 false,那么循环甚至不会运行一次。即使表达式在执行块期间变成了 false,在迭代结束之前也不会停止执行。

The value of the expression is checked each time at the beginning of the loop. If the while expression evaluates to false from the very beginning, the loop won’t even be run once. Even if the expression becomes false during the execution of the block, the execution will not stop until the end of the iteration.

while 循环的语法可以这样表示 −

The syntax of while loop can be expressed as follows −

while (expr){
   statements
}

Example

以下代码展示了一个 while 循环在 PHP 中如何工作的简单示例。变量 $x 在循环开始之前被初始化为 1。只要变量小于或等于 10,循环体就会被要求执行。循环体中的 echo 语句打印当前的迭代次数并增加 x 的值,这样条件最终会变成 false。

The following code shows a simple example of how the while loop works in PHP. The variable $x is initialized to 1 before the loop begins. The loop body is asked to execute as long as it is less than or equal to 10. The echo statement in the loop body prints the current iteration number, and increments the value of x, so that the condition will turn false eventually.

<?php
   $x = 1;

   while ($x<=10) {
      echo "Iteration No. $x \n";
      $x++;
   }
?>

它将生成以下 output

It will produce the following output

Iteration No. 1
Iteration No. 2
Iteration No. 3
Iteration No. 4
Iteration No. 5
Iteration No. 6
Iteration No. 7
Iteration No. 8
Iteration No. 9
Iteration No. 10

请注意测试条件在每次迭代的开头都会被检查。即使条件在循环内变成了 false,执行也会继续到迭代的结尾。

Note that the test condition is checked at the beginning of each iteration. Even if the condition turns false inside the loop, the execution will not stop until the end of the iteration.

Example

在以下示例中,“x” 在每次迭代中都会增加 3。在第三次迭代中,“x” 变成 9。由于测试条件仍然为 true,所以下一轮会发生,“x” 变成 12。由于条件变为 false,所以循环会停止。

In the following example, "x" is incremented by 3 in each iteration. On the third iteration, "x" becomes 9. Since the test condition is still true, the next round takes place in which "x" becomes 12. As the condition turns false, the loop stops.

<?php
   $x = 0;
   while ($x<=10){
      $x+=3;
      echo "Iteration No. $x \n";
   }
?>

它将生成以下 output

It will produce the following output

Iteration No. 3
Iteration No. 6
Iteration No. 9
Iteration No. 12

Example

不一定总是让循环变量增加。如果循环变量的初始值大于循环应该结束时的值,那么需要对其进行减少。

It is not always necessary to have the looping variable incrementing. If the initial value of the loop variable is greater than the value at which the loop is supposed to end, then it must be decremented.

<?php
   $x = 5;
   while ($x>0) {
      echo "Iteration No. $x \n";
      $x--;
   }
?>

它将生成以下 output

It will produce the following output

Iteration No. 5
Iteration No. 4
Iteration No. 3
Iteration No. 2
Iteration No. 1

Iterating an Array with "while"

PHP 中的一个索引数组是一个元素集合,其中每个元素都通过一个从 0 开始的递增索引进行标识。

An indexed array in PHP is a collection of elements, each of which is identified by an incrementing index starting from 0.

你可以通过组成一个 while 循环来遍历一个数组,重复访问 xth 索引处的元素,直到“x”达到数组的长度。这里“x”是一个计数变量,在每次迭代中都会增加。我们还需要一个 count() 函数来返回数组的大小。

You can traverse an array by constituting a while loop by repeatedly accessing the element at the xth index till "x" reaches the length of the array. Here, "x" is a counter variable, incremented with each iteration. We also need a count() function that returns the size of the array.

Example

请看以下示例:

Take a look at the following example −

<?php
   $numbers = array(10, 20, 30, 40, 50);
   $size = count($numbers);
   $x=0;

   while ($x<$size) {
      echo "Number at index $x is $numbers[$x] \n";
      $x++;
   }
?>

它将生成以下 output

It will produce the following output

Number at index 0 is 10
Number at index 1 is 20
Number at index 2 is 30
Number at index 3 is 40
Number at index 4 is 50

Nested "while" Loops

你可以在另一个 while 循环中加入一个 while 循环。外部和内部 while 循环都受两个独立变量控制,这些变量在每次迭代后都会增加。

You may include a while loop inside another while loop. Both the outer and inner while loops are controlled by two separate variables, incremented after each iteration.

Example

<?php
   $i=1;
   $j=1;

   while ($i<=3){
      while ($j<=3){
         echo "i= $i j= $j \n";
         $j++;
      }
      $j=1;
      $i++;
   }
?>

它将生成以下 output

It will produce the following output

i= 1 j= 1
i= 1 j= 2
i= 1 j= 3
i= 2 j= 1
i= 2 j= 2
i= 2 j= 3
i= 3 j= 1
i= 3 j= 2
i= 3 j= 3

要注意“j”是内部 while 循环的计数变量,在它获取所有值之后被重新初始化为 1,因此“i”的下一个值“j”又从 1 开始。

Note that "j" which is the counter variable for the inner while loop is re-initialized to 1 after it takes all the values so that for the next value of "i", "j" again starts from 1.

Traversing the Characters in a String

在 PHP 中,一个字符串可以被认为是有序的字符集合。因此,一个 while 循环,其中一个计数变量从“0”到字符串的长度,可以用来一次获取一个字符。

In PHP, a string can be considered as an indexed collection of characters. Hence, a while loop with a counter variable going from "0" to the length of string can be used to fetch one character at a time.

Example

以下示例统计给定字符串中的元音数量。我们使用 strlen() 来获得长度,使用 str_contains() 来检查字符是否为元音之一。

The following example counts number of vowels in a given string. We use strlen() to obtain the length and str_contains() to check if the character is one of the vowels.

<?php
   $line = "PHP is a popular general-purpose scripting language that is especially suited to web development.";
   $vowels="aeiou";
   $size = strlen($line);
   $i=0;
   $count=0;

   while ($i<$size){
      if (str_contains($vowels, $line[$i])) {
         $count++;
      }
      $i++;
   }
   echo "Number of vowels = $count";
?>

它将生成以下 output

It will produce the following output

Number of vowels = 32

Using the "endwhile" Statement

PHP 还允许你为 while 循环使用替代语法。不是在花括号中组合一个以上的语句,而是使用符号“:”(冒号)来标记条件后面的循环体,并在末尾使用 endwhile 语句。

PHP also lets you use an alternative syntax for the while loop. Instead of clubbing more than one statement in curly brackets, the loop body is marked with a ":" (colon) symbol after the condition and the endwhile statement at the end.

Example

<?php
   $x = 1;
   while ($x<=10):
      echo "Iteration No. $x \n";
      $x++;
   endwhile;
?>

它将生成以下 output

It will produce the following output

Iteration No. 1
Iteration No. 2
Iteration No. 3
Iteration No. 4
Iteration No. 5
Iteration No. 6
Iteration No. 7
Iteration No. 8
Iteration No. 9
Iteration No. 10

请注意 endwhile 语句以分号结束。

Note that the endwhile statement ends with a semicolon.