Home > Article > Backend Development > What are the loop statements in php
The following loop statements are provided in PHP: while: Repeat the code block when the condition is true. do-while: Execute the code block first, then check whether the condition is true, and continue executing the loop if it is true. for: Initialize the variable, check the condition, execute the code block, and then increment the variable. foreach (array): Loops through the elements in the array and provides the key and value. foreach (object): Loops through the properties in the object and provides property names and values.
Loop statements in PHP
PHP provides a variety of loop statements that are used to perform loop statements when certain conditions are met. block of code is executed repeatedly. These loop statements include:
while loop
while (condition)
do-while loop
do {...} while (condition);
for loop
for (initialization; condition; increment)
foreach loop
foreach ($array as $key => $value)
foreach loop (object)
foreach ($object as $property => $value)
break statement
continue statement
Example:
<code class="php">// while 循环 $i = 0; while ($i < 10) { echo $i . "\n"; $i++; } // for 循环 for ($i = 0; $i < 10; $i++) { echo $i . "\n"; } // foreach 循环(数组) $array = ['foo', 'bar', 'baz']; foreach ($array as $item) { echo $item . "\n"; }</code>
The above is the detailed content of What are the loop statements in php. For more information, please follow other related articles on the PHP Chinese website!