How to Use the PHP for Loop – Pi My Life Up

In this tutorial, we will discuss how to use and write a for loop in PHP.

The for loop is an essential control structure within the PHP programming language. The syntax is about the same as most programming languages and is relatively easy to understand.

A for loop is perfect when you know how many times you want to loop through a block of code. Alternatively, you can use a foreach loop instead of a normal for loop if you want to iterate through an array or object.

Unlike a while loop, a for loop accepts three parameters. The first parameter is the creation of our counter variable. The second parameter is the condition. The last parameter is the amount to increase or decrease our counter value in each iteration. As long as the condition is true, our loop will continue to iterate.

Like all types of loops, you can nest for loops as many times as you want. However, it is good practice to keep nested loops to a minimum, as they can be difficult to understand and have high performance if you are processing a large dataset.

Table of contents

for loop syntax How to use a for loop in PHP Write a standard for the loop Exit a for loop Using continue inside a loop for Conclusion for loop syntax

  • The syntax for a for loop in PHP is straightforward and about the same as for loops in most other programming languages. A

for

loop

The code block starts with for followed by three parameters separated by semicolons and parentheses. The loop code block itself must be enclosed with braces.

for (initialize; condition; increment) { // The code here will run at each iteration }

  • Initialize is where the counter variable is created and assigned a value
  • .

  • The condition is the condition that is evaluated in each loop. If the condition equals true, it will continue to loop. Otherwise, if it is fake, it will stop.
  • The increment serves to increase or decrease the value of the counter in each iteration.

How to use a for loop in

PHP

In this tutorial, we go over the basics of writing a valid for loop within the PHP programming language. We’ll also cover topics like skipping an iteration or exiting the loop altogether.

I strongly recommend that you take a little time to learn PHP operators as you will need to understand them to create conditions. Conditional statements are used in various programming control structures, such as if-else statements, switch statements, loops, and more.

Writing a standard for the

loop

In this example, we’ll write a basic for loop to demonstrate how they work in PHP. I’ll quickly review each piece of code in the examples below.

Example

1

We have three parameters in our for loop parenthesis that I will explain quickly

.

  • $x = 1 initializes the loop counter variable and sets its value
  • to 1. $x <= 5 is the

  • condition that the states continue to loop as long as $x is equal to or less than 5
  • .

  • $x++ will increment our $x countervariable by 1 in each iteration.

Within our code block for loop, we simply echo the value of x in each iteration.

<?php for($x = 1; $x <= 5; $x++) { echo “The value of x is ” . $x . “.<br>”; } ?>

Below is the output of the above code. As you can see, our loop continued to iterate until $x no longer matched the condition because it was greater than 5.

The value of x is 1. The value of x is 2. The value of x is 3. The value of x is 4. The value of x is 5.

Example 2

In this example, we decrease

our counter variable and use larger values. $y = 100 initializes our counter variable and sets the value to 100. $y >= 100 is the

  • condition that the states continue to loop as long as $y is equal to or greater than
  • 50. $y -= 10

  • will decrease our $y countervariable by
  • 10 in each iteration. In each iteration, we echo the value of $y along with the line break HTML element. <?php for($y =

  • 100
  • ; $y >= 50; $y -= 10

) {

echo

“The value of y is ” . $y . “.<br>”; } ?>

The following output shows that each loop iteration decreases our $y variable. Once $y no longer matches the condition that it is greater than or equal to 50, the loop is completed.

The value of y is 100. The value of y is 90. The value of y is 80. The value of y is 70. The value of y is 60. The value of y is 50.

Exiting a

for loop is the same as every loop with PHP. All you need to do is use the break statement. You will need to place the break statement inside a conditional block, so it is only executed when certain requirements are met.

In our example below, we walk until $z equals 3. Once the condition is met, we hit our break statement and exit the loop.

<?php for($z = 1; $z <= 3; $z++) { if($z == 3) { echo “Script break.”; break; } echo “The value of z is ” . $z . “.<br>”; } ?>

The result clearly demonstrates the behavior of using the break statement. Once $z was equal to 3, the loop exited and PHP reached the end of our script.

The value of z is 1. The value of z is 2. Breaking the script. Using continue

Inside a

for

Loop You can end an iteration of the for loop ahead of time by using the continue statement. Skipping an iteration can be important in cases where you don’t want a particular dataset to be processed, and it’s safe to continue to the next iteration.

In our example below, we’ll skip the iteration whenever $x equals

3. <?php for($x = 1; $x <= 5; $x++) { if($x == 3) { echo “Skipping.<br>”; continue; } echo “The value of x is ” . $x . “.<br>”; } ?>

As you can see in the output below, when $x was equal to 3, we printed “skip” instead of the value of $x. The for loop continued to iterate until it no longer matched the condition within the parenthesis of the for loops.

The value of x is 1. The value of x is 2. Jump. The value of x is 4. The value of x is 5.

Conclusion

You’ll likely find yourself using a lot for loops in PHP programming, so they’re incredibly important to understand. I hope this guide was able to teach all the basics in a for loop and that you can now read and write them.

The for loop isn’t the only one you can use within PHP. Other loops, such as a while loop or foreach loops, are extremely useful but have pros and cons. I strongly recommend that you take the time to learn as many control structures as possible, as they can help dramatically improve the readability and performance of your code.

If we can improve this tutorial, feel free to let us know by leaving a comment below.