Free cookie consent management tool by TermsFeed Generator PHP Loops | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Loops

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:

  1. for
  2. while
  3. do...while
  4. 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?

https://youtu.be/osQFwoM0Amo

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

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP xml and json
Mar 21, 2023 programming

PHP xml and json

Today we are going to talk about working XML and JSON in PHP. XML and JSON are the most common formats that are used in APIs. knowing how to work with them is essential for any web developer. ...

9 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
A Beginner's Guide to Different Types of Software Testing
Apr 07, 2024 programming

A Beginner's Guide to Different Types of Software Testing

Software testing is a crucial aspect of programming and software development. By testing, developers can ensure that their software performs as expected. ...

9 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...