This cheat sheet provides a quick reference guide for various types of loops in PHP. Loops are essential for executing a block of code repeatedly, allowing for efficient and streamlined programming. The following sections outline the syntax and provide examples for each type of loop, along with the usage of break and continue statements.
For loop
The for loop is ised to execute a block of code a specific number of times.
Syntax
for (initialization; condition; increment/decrement) {
// Code to be executed
}
Example
for ($i = 0; $i < 5; $i++) {
echo "The number is: $i <br>";
}
Output
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
While loop
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition) {
// Code to be executed
// Increment or decrement variable inside the loop if necessary
}
Example
$i = 0;
while ($i < 5) {
echo "The number is: $i <br>";
$i++;
}
Output
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
Do-While loop
The do-while loop is similar to the while loop, but the code is executed at least once before the condition is checked.
Syntax
do {
// Code to be executed
// Increment or decrement variable inside the loop if necessary
} while (condition);
Example
$i = 0;
do {
echo "The number is: $i <br>";
$i++;
} while ($i < 5);
Output
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
Foreach loop (for arrays)
The foreach loop (for arrays) iterates through each element in an array.
Syntax
foreach ($array as $value) {
// Code to be executed
}
Example
$colors = array("Red", "Green", "Blue");
foreach ($colors as $value) {
echo "$value <br>";
}
Output
Red
Green
Blue
Foreach loop (for associative arrays)
The foreach loop (for associative arrays) Iterates through each element in an associative array, providing both the key and the value.
Syntax
foreach ($array as $key => $value) {
// Code to be executed
}
Example
$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");
foreach ($age as $key => $value) {
echo "$key is $value years old <br>";
}
Output
Peter is 35 years old
Ben is 37 years old
Joe is 43 years old
Break statement
The break statement terminates the loop when a specified condition is met.
Syntax
Syntax example using a for loop and if statement.
for (initialization; condition; increment/decrement) {
if (condition) {
break; // Terminate the loop
}
}
Example
In this example, the for loop will terminate when the variable ‘i’ reaches 3.
for ($i = 0; $i < 5; $i++) {
if ($i == 3) {
break; // Terminate the loop
}
echo "The number is: $i <br>";
}
Output
The number is: 0
The number is: 1
The number is: 2
Continue statement
The continue statement skips the current iteration of a loop based on a specific condition.
Syntax
Syntax example using a for loop and if statement.
for (initialization; condition; increment/decrement) {
if (condition) {
continue; // Skip this iteration
}
// Code to be executed
}
Example
This example will output the numbers from 0 to 4, skipping the number 3.
for ($i = 0; $i < 5; $i++) {
if ($i == 3) {
continue; // Skip this iteration
}
echo "The number is: $i <br>";
}
Output
The number is: 0
The number is: 1
The number is: 2
The number is: 4