Php 简明教程

PHP – Loop Types

PHP 中的循环用于对同一代码块执行指定次数。PHP 支持以下四种类型的循环。

Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types.

  1. for − Loops through a block of code a specified number of times.

  2. foreach − Loops through a block of code for each element in an array.

  3. while − Loops through a block of code if and as long as a specified condition is true.

  4. do-while − Loops through a block of code once, and then repeats the loop as long as a special condition is true.

此外,我们还将说明在 PHP 中如何使用 continuebreak 语句来控制循环的执行。

In addition, we will also explain how the continue and break statements are used in PHP to control the execution of loops.

PHP for Loop

当你清楚知道要执行一条或多条语句的次数时,就可以使用 for 语句。

The for statement is used when you know how many times you want to execute a statement or a block of statements.

php for loop

Syntax

for (initialization; condition; increment){
   code to be executed;
}

初始值设定程序用于设置循环迭代计数器的起始值。为此目的,此处可以声明一个变量,而按传统,此变量被命名为 $i。

The initializer is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i.

Example

以下实例进行五次迭代,并在循环的每次遍历中更改两个变量的赋值——

The following example makes five iterations and changes the assigned value of two variables on each pass of the loop −

<?php
   $a = 0;
   $b = 0;

   for( $i = 0; $i<5; $i++ ) {
      $a += 10;
      $b += 5;
   }

   echo ("At the end of the loop a = $a and b = $b" );
?>

它将生成以下 output

It will produce the following output

At the end of the loop a = 50 and b = 25

PHP foreach Loop

foreach 语句用于循环遍历数组。每次遍历时,当前数组元素的值分配给 $value,数组指针移动一位,在下次遍历中,将处理下一个元素。

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

Syntax

foreach (array as value) {
   code to be executed;
}

Example

尝试以下实例列出数组的值。

Try out following example to list out the values of an array.

<?php
   $array = array( 1, 2, 3, 4, 5);

   foreach( $array as $value ) {
      echo "Value is $value \n";
   }
?>

它将生成以下 output

It will produce the following output

Value is 1
Value is 2
Value is 3
Value is 4
Value is 5

PHP while Loop

while 语句会在测试表达式为 true 的情况下执行代码块。

The while statement will execute a block of code if and as long as a test expression is true.

如果测试表达式为 true,则会执行代码块。代码执行后,测试表达式将再次进行评估,并且循环将继续进行,直到发现测试表达式为 false。

If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.

php while loop

Syntax

while (condition) {
   code to be executed;
}

Example

此示例在循环的每次迭代中递减变量值,并且计数器会递增,直到达到 10(此时评估为 false)并结束循环。

This example decrements a variable value on each iteration of the loop and the counter increments until it reaches 10 when the evaluation is false and the loop ends.

<?php
   $i = 0;
   $num = 50;

   while($i < 10) {
      $num--;
      $i++;
   }

   echo ("Loop stopped at i = $i and num = $num" );
?>

它将生成以下 output

It will produce the following output

Loop stopped at i = 10 and num = 40

PHP do-while Loop

do-while 语句会至少执行代码块一次——然后,只要某个条件为真,它就会重复循环。

The do-while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.

Syntax

do {
   code to be executed;
}
while (condition);

Example

以下示例会至少递增一次 i 的值,并且只要 i 的值为小于 10,它就会继续递增 i 的变量——

The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 10 −

<?php
   $i = 0;
   $num = 0;

   do {
      $i++;
   }

   while( $i < 10 );
   echo ("Loop stopped at i = $i" );
?>

它将生成以下 output

It will produce the following output

Loop stopped at i = 10

PHP break Statement

PHP break 关键字用于过早终止循环的执行。

The PHP break keyword is used to terminate the execution of a loop prematurely.

break 语句位于语句块内。它会为你提供完全的控制权,无论何时你想要退出循环,都可以退出。退出循环后,会执行循环的后续语句。

The break statement is situated inside the statement block. It gives you full control and whenever you want to exit from the loop you can come out. After coming out of a loop immediate statement to the loop will be executed.

cpp break statement

Example

在以下示例中,当计数器值达到 3 时,条件测试变为 true,并且循环终止。

In the following example condition test becomes true when the counter value reaches 3 and loop terminates.

<?php
   $i = 0;

   while( $i < 10) {
      $i++;
      if( $i == 3 )break;
   }
   echo ("Loop stopped at i = $i" );
?>

它将生成以下 output

It will produce the following output

Loop stopped at i = 3

PHP continue Statement

PHP continue 关键字用于中止循环的当前迭代,但不会终止循环。

The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.

break 语句类似, continue 语句位于包含循环执行的代码的语句块内,在语句块前面是一个条件测试。对于遇到 continue 语句的遍历,将跳过循环代码的剩余部分,然后开始下一次遍历。

Just like the break statement the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. For the pass encountering continue statement, rest of the loop code is skipped and next pass starts.

cpp continue statement

Example

在以下示例中,循环打印数组的值,但是对于条件变为真的值,它只跳过代码,然后打印下一个值。

In the following example loop prints the value of array but for which condition becomes true it just skip the code and next value is printed.

<?php
   $array = array( 1, 2, 3, 4, 5);

   foreach( $array as $value ) {
      if( $value == 3 )continue;
      echo "Value is $value \n";
   }
?>

它将生成以下 output

It will produce the following output

Value is 1
Value is 2
Value is 4
Value is 5