Loop in PHP
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
May be interested
- For ... loop in JavaScriptthe for ... in loop is used to iterate over the properties of an object. when we haven't discussed the object yet, you may not feel comfortable with this loop. but once you understand how objects work in javascript, you will find this loop very useful.
- Loop control in JavaScriptjavascript provides high control to handle loop commands and switch commands. there may be a situation when you need to exit the loop without having to go to its endpoint. there may also be situations when you want to jump over a part of the code block and start the next loop.
- Why Microsoft Loop Can't Compare to Notionmany people have been using notion for brainstorming for years, so it's interesting to see if loop can replace it as they move deeper into the microsoft ecosystem.
- How to fix 'Please Wait for the GPSVC' loop errorthe please wait for the gpsvc loop error in windows is an annoying problem that can cause the system to get stuck at shutdown. this loop is related to group policy client service (gpsvc).
- The while loop in Shellthe while loop gives you the ability to execute a set of repetitive commands until certain conditions occur. it is often used when you need to manipulate iterative variable values.
- Until loop in Shellthe while loop is perfect for the situation where you want to execute a set of commands while some conditions are true. sometimes you need to execute a set of commands until a condition is true, then you need to use a until loop.
- Select loop in Shellthe select loop provides an easy way to create a numbered menu from which the user can select. it is useful when you want to ask the user to select one or more items from a list of options.
- For loop in Shellthe for loop works on lists of items (items). it repeats a set of commands for each item in a list.
- Loop in Unix / Linuxa loop is a program that has powerful features, allowing you to repeat a set of commands. in this chapter, you will learn through practical examples of loops contained in shell programs.
- How to troubleshoot Refresh Loop in Microsoft Store on Windows 10microsoft store is not necessarily the most stable application on windows 10. this application is not problematic but the download is often faulty, applications cannot be installed or updated. sometimes, microsoft store will not exit the main screen. there are some basic solutions to these common problems: uninstalling and reinstalling the application. a fairly new issue that has not yet been noted is the microsoft store refresh loop.