Loop in PHP
The loop in PHP is used to execute the same block of code some time specified.
The loop in PHP is used to execute the same block of code some time specified. PHP supports the following 4 types of loops:
- For loop - Repeat through a block of code some times identified.
- While loop - iterates over a code block if and as long as a condition is defined as true.
- Do . while - loop iterates through a code block once, and then repeats that loop as long as a condition is true.
- Foreach loop - Repeats through a code block for each element in an array.
At the end of the chapter, we will discuss the keywords continue and break to control the execution of loops in PHP.
For loop in PHP
The for command is used when you know in advance how many times you want to execute a command or a block of commands in PHP.
Syntax
for ( khởi_tạo ; điều_kiện ; tăng_giảm_giá_trị ) { phần code để thực thi }
The initialization is used to set the start value for the iteration count variable. A variable can be declared here for this purpose and its traditional name is $ i.
For example
The following example creates 5 iterations and changes the assigned value of the two variables each iteration.
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
While loop in PHP
The while statement in PHP will execute a code block if and as long as a condition is defined as true.
If the condition is true, then the code block will be executed. After the code is executed, the conditional expression will be evaluated again, and the loop continues executing until the conditional expression is false.
Syntax
while (thing_kater)
{
The code is executed if the condition is true
}
For example
The following example reduces the value of a variable through each iteration and increases the variable value counting until it reaches 10, then the estimate is false and the loop ends.
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Do . while loop command in PHP
The do . while command will execute a code block at least once. It will then repeat the loop as long as the condition is true.
Syntax
by
{
code to execute
}
while (thing_kien);
For example
The following example will output the value of i at least once, and then it will continue to increase variable i as long as it has a value less than 10.
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Foreach loop command in PHP
The foreach loop command is used to iterate over an array. For each iteration, the value for the current array element is assigned to $ value and the array pointer is moved to the next element and in the next iteration the next element is processed.
Syntax
foreach (array as value)
{
code to execute
}
For example
Try the following example to list the values of an array.
";
}
?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Break command in PHP
Break keywords in PHP are used to end the execution of a sudden loop.
Break command is placed inside the block. It gives you complete control whenever you want to exit the loop. After exiting the loop, the command immediately after that loop will be executed.
For example
The following example checks the true condition when the variable value counts = 3 and the loop ends.
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
The continue command in PHP
The continue keyword in PHP is used to stop the current loop but not the end of that loop.
Like the break statement, the continue statement is placed inside the code block containing the code that the loop executes, preceded by a conditional test expression. When the continue statement is encountered, the rest of the loop is ignored and the next loop continues.
For example
The following loop prints out the value of an array, if it satisfies the condition, it prints, otherwise omits and continues the loop.
";
}
?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Follow tutorialspoint
Previous lesson: if, else, switch commands in PHP
Next post: Array (Array) in PHP
You should read it
- The loop technique in Python
- WHILE loop in SQL Server
- FOR loop in SQL Server
- C ++ loop
- 11 best tips to get started with Microsoft Loop
- Loop control in JavaScript
- The while loop in JavaScript
- Loop in programming C
- How to Fix Boot Loop Problems in Windows
- While loop in Python
- Control loop in Unix / Linux
- What is Loop mail?
Maybe you are interested
What is Large Action Model (LAM)? SpaceX's Crew Dragon spacecraft successfully assembled with the ISS station, completely automated Will NASA partner with Tesla to create the most unprecedented lunar exploration vehicle? SpaceX is about to blow up a tens of millions of dollars worth of boosters on the Atlantic Life is amazing: The process of transforming from a living cell to a perfect living organism in just 6 minutes NASA announces a stunning series of GIF images that capture the historic moments of the aerospace industry