for, while, and foreach loops php
In PHP program development, for, while, and foreach loops are used to repeat a block of program source code multiple times based on a particular condition. PHP programming provides its users with support for several types of loops, primarily for, while, and foreach looping features. The use of each loop in every PHP program is helpful for multiple programming condition situations. The use of a loop in PHP programs depends on how the user wants to print statements multiple times through the loop in their program.

For Loop in PHP.
For loops are used in PHP program development when programmers know a pre-given or repeating program condition, allowing them to determine how many times a block of program source code should be executed or repeated. Where the for loop helps the programmer repeat or display a series of numbers.
For Loop Syntax.
for (initialization; condition; increment) {
/ Program code to be executed and print a statement
}
For Loop Explosion.
- Loop initialization – This is the starting counter in a for loop, which initializes a variable.
- Condition – This is a Boolean expression or condition in a for loop that is checked before each iteration in every execution. If the condition is true, the for loop will continue; if the condition is false, the loop will automatically stop.
- Increment – Here, the loop updates the counter value after each for loop iteration, where the counter value is incremented or decremented as long as the condition is true.
Example of a For Loop.
<?php
// Here the p variable prints numbers from 1 to 10
for ($p = 1; $p <= 10; $p++) {
echo $p . ” “; // Result – 1 2 3 4 5 6 7 8 9 10
}
?>
Here in the For Loop example.
- Initialization – Here $p = 1 — The for loop starts at 1.
- for Condition – $p <= 10 — The for loop runs as long as $p is less than or equal to 10.
- Increment – Here $p++ — Increases the counter by 1 with each loop repetition.
While Loop in PHP.
The while loop is used in PHP program development when PHP programmers want to repeat a block of program source code when a particular condition is true. Where the program’s while loop condition is checked before each iteration. If the condition given at the start of the program is false, the while loop will not execute.
The syntax of a while loop.
while (condition) {
/ // check the condition if it is true, the code will be executed.
}
While loop condition – This is a Boolean expression in the while loop, which is checked before each program iteration. If the condition is true, the while loop runs; if the condition is false, the loop stops automatically.
Example of a while loop.
<?php
$p = 1;
// Here, the p variable prints numbers from 1 to 10
while ($p <= 10) {
echo $p . ” “; // Result – 1 2 3 4 5 6 7 8 9 10
$p++; // it increments the p variable counter
}
?>
Here’s an example of a while loop.
- While loop condition – $p <= 10 — The loop runs as long as $p <= 10 is less than or equal to 10.
- In the while loop, $p++ increments $p. Finally, the condition breaks.
- If the starting value of $p was already greater than 10, the while loop would not run at all, as the condition would be false from the start.
Foreach Loop in PHP.
In PHP program development, the foreach loop is used to iterate over array data types in a specific order. The foreach loop allows the user to loop through each data element of an array or object from start to end, processing its values. The foreach loop is a common loop applied to arrays in PHP programming.
Syntax of a Foreach Loop for Arrays.
foreach ($array as $value) {
// It is used to code to execute with each $element in the array from start to end
}
Using a foreach loop, users can access both the keys and array data values in an array.
foreach ($array as $key => $value) {
// It is used to code to execute with each $key and $value from start to end
}
Example of a foreach loop with array iteration.
<?php
$programmings = [“php”, “javascript”, “python”, “java”];
foreach ($programmings as $programming) {
echo $programming . ” “; // Result – php javascript python java
}
?>
Example of a foreach loop.
Here the foreach loop iterates over each element of the $programmings array, and each array element points to the value $programming.
Here the foreach loop iterates over the complete array automatically in order.
Example of an associative array with foreach keys and values.
<?php
$employee = [
“emp_name” => “Harry deora”,
“emp_age” => 21,
“emp_cont” => 9414,
“address” => “india”
];
foreach ($employee as $key => $value) {
echo $key . ” – ” . $value . “\n”;
}
// Result –
// emp_name – Harry deora
// emp_age – 21
//emp_cont – 9414
//address – india
?>
Associative array with foreach keys and values example.
The PHP program loop iterates over an associative array and displays information containing keys (employee_name, employee_age, employee_contact, and employee_address) and values (emp_name – Harry deora, emp_age – 21, emp_cont – 9414, address – india).
Example of a PHP program combinational loop.
Nested Loops In a PHP program, using a loop within a loop is called a nested loop. For example, using a for loop within a while loop is an example of a nested loop.
<?php
$p = 1;
while ($p <= 4) {
echo “nested while loop $p\n”;
for ($q = 1; $q <= 4; $q++) {
echo “nested for loop $q\n “;
}
$p++; // Here it increments the outer loop counter until the condition is false
}
?>
In the example of a nested loop.
Here the nested while loop runs 4 times, and for each repetition of the while loop, the for loop runs 4 times.
Example of foreach inside a for loop in PHP.
<?php
$course = [
“php” => 999,
“java” => 1999,
“python” => 1199
];
for ($i = 0; $i < 2; $i++) {
echo “nested loop repetition $i:\n”;
foreach ($course as $course_name => $price) {
echo “$course_name course price is – $price \n”;
}
}
?>
In the example of a nested foreach.
In this nested loop example, the for loop executes twice, and for each repetition of the for loop, the foreach loop iterates over $course as an associative array.
Comparison summary of for, while, and foreach loop in php
| Type of loop | When to Use each loop | Key Features of each loop |
| For loop | Use for loop When you know the number of iterations in advance in any program or logic expression. | For loop Ideal for iterating over a range of numbers (like -1 to 10). |
| While loop | Use while loop in program When you need to repeat a block of code as long as a condition is true in given expression. | While loop Useful for situations where the number of iterations is not known by programmer. |
| Foreach loop | Use foreach loop Specifically for iterating over arrays or objects element from start to end. | Foreach loop Automatically handles array elements, making it ideal for arrays and object data type. |
Conclusion of for, while, and foreach loops in PHP.
- For loop – The most commonly used loop in PHP program development. use the for loop when the programmer knows the number of numbers or values to be repeated in a program, or when you need to print a series of numbers.
- While loop – The while loop is used in PHP program development when you want to run the loop only when a condition is true, and the user doesn’t know the number of repetitions in advance.
- Foreach loop – The foreach loop is ideal for performing repetitions on array-indexed or associative arrays and objects in PHP program development, as it simplifies working with array and object data collections.

