PHP Loops
Last Updated on Mar 21, 2023
Why do we need loops?
Sometimes we want to run a piece of code over and over again or apply some changes to every item in an array. Rather than repeating the same code, we will run the code through a loop.
For example let’s assume I want to echo "hello world" 10 times;
echo 'hello world';
echo 'hello world';
echo 'hello world';
echo 'hello world';
echo 'hello world';
echo 'hello world';
echo 'hello world';
echo 'hello world';
echo 'hello world';
echo 'hello world';
I can use a loop that echoes 'hello world' for me as many times as I want without repeating myself.
Now that you see how important and useful, loops are, let’s talk about the loops in PHP.
Types of Loops
There are 4 types of loops in PHP:
- for
- while
- do...while
- foreach
1. for
for ($x = 0; $x <= 5; $x++) {
echo "value of x is $x";
}
Here $x is called the counter. First we initialize it.
$x = 0;
Then we test the counter. Here we are testing if it’s less than or equal to 5. If it is then we go inside the loop and run the code otherwise we exit the loop
$x <= 5;
and then we change the counter.
$x++
After running the code inside the loop, we change the counter.
Here we add 1 to it. Sometimes you want to minus 1 from it. You can multiply it. You can do anything you want with it. But it’s important that you change it. Otherwise you will get stuck in the loop.
The output of this loop is
value of x is 0
value of x is 1
value of x is 2
value of x is 3
value of x is 4
value of x is 5
After that because $x becomes 6 and 6 <= 5 outputs false we exit the loop.
2. while
while (condition) {
echo "here";
}
At first it checks the condition if it’s true it runs the code inside the loop and as long as the condition is true it keeps running the code inside the loop.
One of the most common mistake with this code is not changing the condition. That causes the loop to run infinitely and it never stops. And as a result your program crashes.
$number = 2;
while($number < 5){
echo "number is now $number";
}
This loop will run forever.
Inside the loop you should change the variable that is being tested inside the condition. For example we should change it to something like this:
$number = 2;
while($number < 5){
echo "number is $number";
$number++;
}
The first one is 2, then it changes to 3,t hen to 4 and then when it changes to 5 the condition is false and it exits the loop.
3. do...while
do {
// code to be executed;
} while (condition is true);
It’s like while loop with 1 difference. In while loop we check the condition first if it was true then we run the code. So if the condition is false we will never run the code inside the loop.
But with do...while loop we run the code inside the loop at least once no matter the condition is true or false. We run it once then we check the condition if the condition is false then we exit the loop but it’s true we run it again.
4. foreach
foreach ($array as $value) {
// code to be executed;
}
or
foreach ($array as $key => $value) {
// code to be executed;
}
It’s a great way to loop through each item in an array. So rather than
for($i = 0;$i < sizeof($array);$i++){
$array[$i];
}
We can write
foreach($array as $value){
$value;
}
See how more readable and easier it is?
Conclusion
Now you know about loops in PHP.
I recommend you to open a PHP file and write different loops. have a false condition and see if you go inside a loop or not.
If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!
Key takeaways
- what are loops
- different types of loops in PHP
- writing a for loop
- writing a while loop and how to avoid getting stuck in the loop forever
- writing a do...while loop and its difference with while loop
- writing a foreach loop