Loops in PHP
To avoid repeating the same code snippets multiple times, loops are used. So, how many kinds of loops are there in PHP, and how are they used? Let's explore right away.
I. Why Use PHP Loops
When you write code with many repetitive sections, you can't simply copy and paste the same line or block of code multiple times because it can easily lead to syntax errors and is very time-consuming. For this reason, most programming languages have loop features to help you solve this problem.
In PHP, there are the following types of loops:
- while - the loop continues to run as long as the condition is true.
- do ... while - a variant of the while loop, which checks the condition at the end of each iteration.
- for - loop through a block of code a specified number of times.
- foreach - loop through a block of code for each element in an array.
II. While Loop
The loop continues to run as long as its condition is true.
1. Syntax
while (condition is true){
the code is executed;
}
2. Example
Here is an example: Print numbers from 1 to 10.
$x = 1;
while($x <= 10) {
echo "This is the number: $x
";
$x++;
}
While running the code above, it will check the condition. If the condition is true, the code will execute. Then it will check the condition again, and if still true, execute the code again. This continues until the condition is false. In other words, it checks the condition first before running the code. The variable $x is increased by 1 after each iteration, and when $x equals 11, the condition is no longer true, and the while loop ends.
III. Do ... While Loop
The do ... while loop is a variant of the while loop that checks the condition at the end of each loop iteration.
1. Syntax
do {
code will execute
} while (condition is true);
2. Example
Here is an example: Print numbers from 1 to 10.
$x = 1;
do {
echo "This is the number: $x
";
$x++;
} while ($x <= 10);
The loop starts with $x = 1, then it prints the output and increments $x by one. Next, it is evaluated by the condition, and the loop will continue as long as $x is less than or equal to 10.
IV. For Loop
The for loop is used when you know how many times a block of instructions will run in advance.
1. Syntax
for (init counter; test counter; increment counter) {
execute code;
}
Explanation:
+ init counter: Initialize the loop counter value
+ test counter: Check the condition. If it is TRUE, the loop continues. If it is FALSE, the loop ends.
+ increment counter: Increase the loop counter value.
2. Example
Here's an example: Print numbers from 1 to 10.
for ($x = 1; $x <= 10; $x++) {
echo "This is the number: $x
";
}
The loop starts with $x = 1, and continues until $x = 11, at which point it will stop. $x is incremented after each iteration.
V. Foreach Loop
The foreach loop is used to iterate over arrays.
1. Syntax
foreach ($array as $value) {
executable code;
}
2. Example
Here's an example: Print each element in the array $array = ["a", "b", "c", "d"].
$array = ["a","b","c","d"];
foreach ($array as $item {
echo "Element : " . $item . "\n";
}
To get the keys' values of each element in the array, use the following extended syntax.
foreach ($array as $key => $value) {
executable code;
}
VI. Break and Continue Keywords
1. Break Keyword
The break keyword is used to terminate the execution of a loop prematurely.
Example: Print numbers from 1 to 10. Exit the loop when it reaches the number 4.
for ($x = 1; $x <= 10; $x++) {
if ($x == 4) {
break;
}
echo "This is the number: $x
";
}
2. Continue Keyword
The continue keyword is used to skip the current iteration without stopping the whole loop like break.
Example: Print numbers from 1 to 10. Do not print the number 4.
for ($x = 1; $x <= 10; $x++) {
if ($x == 4) {
continue;
}
echo "This is number: $x
";
}
In conclusion, the continue keyword is similar to break, but it only ends the current iteration early, not the entire loop.
Submit feedback
Your email address will not be made public. Fields marked are required *
Search
Trend
-
What is Black Myth: Wukong? Detailed Guide on System Requirements and Gameplay
08-21-2024 . 771 view
-
Call of Duty: Black Ops 6 - Intense, Mysterious, and Surprising Warfare
09-02-2024 . 677 view
-
The "End of Life" for Windows 10: A Massive E-Waste Threat and Sustainable Solutions
08-18-2024 . 651 view
-
Casio WS-B1000: The Perfect Blend of Traditional Watch and Smartwatch
08-11-2024 . 576 view
-
Call of Duty: Black Ops 6 Consumes 300GB SSD: Xbox Series X/S Gamers Take Note
09-02-2024 . 539 view
0 feedback