PHP has three instructions to implement loops: 1) for loop; 2) while loop; 3) do...while loop.
Instructions to implement loops in PHP
There are three main instructions to implement loops in PHP:
<code class="php">for ($i = 0; $i < 10; $i++) { // 循环体 }</code>
<code class="php">while ($condition) { // 循环体 }</code>
<code class="php">do { // 循环体 } while ($condition);</code>
Detailed description:
for loop
for loop Is the most commonly used loop structure, which uses three clauses to define the loop:
while loop
The while loop will execute the loop body as long as the specified condition is true. If the condition is false at the beginning of the loop, the body of the loop will not execute.
do...while loop
do...while loop is similar to while loop, but it will execute the loop body at least once, even if the condition is at the beginning of the loop is false.
Usage example:
<code class="php">// for 循环 for ($i = 0; $i < 10; $i++) { echo "$i "; } // while 循环 $j = 0; while ($j < 10) { echo "$j "; $j++; } // do...while 循环 $k = 0; do { echo "$k "; $k++; } while ($k < 10);</code>
Output:
<code>0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9</code>
The above is the detailed content of What are the instructions that can implement looping in php?. For more information, please follow other related articles on the PHP Chinese website!