PHP Loop - For Loop
Loops through a block of code a specified number of times, or when a specified condition is true.
for loop
The for loop is used when you know in advance the number of times the script needs to run.
The for loop can actually be seen as a compact and concise version of the while loop
for (expr1; expr2; expr3)
statement
for The loop has three expressions
• The first expression expr1 is executed only once at the beginning of the loop
• The second expression expr2 is executed at the beginning of each loop of the loop body , if the execution result is true, the loop continues, if it is found to be false, the loop ends
• The third expression expr3 is executed at the end of each loop of the loop body
Syntax
for (initial value; condition; increment)
{
Code to be executed;
}
Parameters:
· Initial value: Mainly initializes a variable value, used to set a counter (but it can be any code that is executed once at the beginning of the loop).
· Conditions: Restrictions of cycle execution. If TRUE, the loop continues. If FALSE, the loop ends.
· Increment: Mainly used to increment the counter (but can be any code that is executed at the end of the loop).
Note: The above initial value and increment parameters can be empty, or have multiple expressions (separated by commas).
Example
The following example defines a loop with an initial value of i=1. As long as the variable i is less than or equal to 5, the loop will continue to run. Each time the loop runs, the variable i will be incremented by 1:
<html> <body> <?php for ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br>"; } ?> </body> </html>
Review the code of the while loop
$num = 1;
while ($num < = 5) {
echo $num;
$num++;
}
If you use a for loop, you can change it to
for ($num = 1; $num <= 5; $num++) {
echo $num;
}
Therefore, in terms of functionality, the for loop and the while loop can be regarded as equivalent
foreach loop
The foreach loop is used to traverse the array.
In fact, PHP also has a special loop statement, which is specially used to traverse arrays
foreach (array_variable as val)
statement
array_variable represents an array variable. During each loop execution, the value of each element will be temporarily assigned to the variable val. The value of val obtained by the statement statement is different each time. Experience the following code
foreach ($arr as $item) {
echo "<" . $item . ">";
}
Syntax
foreach ($array as $value)
{
To execute code;
}
Each time the loop is performed, the value of the current array element will be assigned to the $value variable (the array pointer will move one by one ), on the next loop you will see the next value in the array.
Example
The following example demonstrates a loop that outputs the values of a given array:
<html> <body> <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value . "<br>"; } ?> </body> </html>
Foreach has another usage, definition As follows
foreach (array_variable as key => val)
statement
key represents the subscript of the array, and val represents the value of the array, then for the numerical subscript For arrays, the value of key in each loop is the number that increases from 0.
break and continue
If we want to stop the execution of the code in the looped code block, we can use break or continue
• have break "break" – if you want to stop the execution of the code in the loop code block. , and the loop will no longer be executed
• Continue to Execute, and enter the next cycle. It's a break. I lifted the chessboard and stopped playing. This is a break.
Some people have a good mentality. When they see that they are about to lose, they give up and give up and try another game. This is continue.