PHP Lessons - Lesson 12 - While, foreach, for loops
Quite often in programming — and even in everyday life — we need to perform repetitive tasks until we’re satisfied with the result. In everyday life, we might call this a chore. In programming, it’s called a loop. Loops are very common in PHP because they are a convenient way to iterate through arrays, calculate functions, and perform a variety of other operations. Let’s begin with the simplest and most popular loop: while
.
PHP while
Loops
The while
loop is very straightforward. Inside parentheses, we write a condition; inside curly braces, we put the code to execute while that condition is true. The condition is a logical expression that evaluates to either TRUE
or FALSE
. (If you want to brush up on logical expressions, check out the lesson on the if
operator.)
Example of while
loop:
<?php $counter = 5; $newArray = array(); while($counter > 2){ $newArray[] = $counter; $counter--; } print_r($newArray); ?>
This will output the array: [5, 4, 3]
. Let’s break down what happens:
- Step 1: $counter = 5 (5 > 2)
- Step 2: $counter = 4 (4 > 2)
- Step 3: $counter = 3 (3 > 2)
- Step 4: $counter = 2 (2 == 2, condition fails, loop ends)
Now let’s look at a function example: tabulating values of the function y(x) = 2x - 1:
<?php $x = -10; while($x <= 10){ $y = 2 * $x - 1; print 'y(' . $x . ') = ' . $y . '<br />'; $x++; } ?>
This will output a list of values for x from -10 to 10.
This is the flowchart of a while
loop. The loop runs as long as the condition is true
. As soon as it becomes false
, the program exits the loop.
Another IMPORTANT note: if you remove the line $x++
, the condition will always be true and the loop will never end:
<?php $x = -10; while($x <= 10){ $y = 2 * $x - 1; print 'y(' . $x . ') = ' . $y . '<br />'; // $x++; } ?>
This is known as an infinite loop, and it can cause your website to hang or crash. Always make sure your loops have an exit condition!
PHP foreach
Loops
foreach
is extremely useful when working with arrays. It allows us to loop through each element one at a time, performing actions on each one.
Example: count how many elements are "red" in an array:
<?php $counter = 0; $newArray = array( 1 => 'red', 2 => 'blue', 3 => 'blue', 4 => 'red', 5 => 'red', 6 => 'blue', 7 => 'red', 8 => 'blue', 9 => 'red', 10 => 'blue', ); foreach($newArray as $key){ if($key == 'red'){ $counter++; } } print 'We have ' . $counter . ' red elements'; ?>
Simple enough, right? foreach
iterates over each element until the array ends.
Now let’s work with a multidimensional array (array of arrays):
<?php $counter = 0; $newArray = array( 1 => array(1 => 'red', 2 => 'blue', 3 => 'blue'), 2 => array(1 => 'red', 2 => 'red', 3 => 'blue'), ); foreach($newArray as $subArray){ foreach($subArray as $element){ if($element == 'red'){ $counter++; } } } print 'We have ' . $counter . ' red elements'; ?>
foreach
is handy and you’ll see it often in PHP examples and real projects.
PHP for
Loops
for
is similar to while
but is often used when we know exactly how many times to repeat the loop (e.g., 10 or 100 times).
for($i = 0; $i < 10; $i++){ // this runs 10 times }
Explanation of each part:
$i = 0
— loop counter initialization.$i < 10
— the loop will run while this condition is true.$i++
— increment the counter at each step.