For Loop In PHP

In this article, we will explain what are PHP for loop and how to use it with several examples.

With For loops, you can run a block of code a fixed number of times in PHP.

In this type of loop, you will know in advance how many times to execute the loop.



PHP for loop is usually used when the number of times the script should run is known beforehand.

Using the for loop, you are able to execute a block of code a specific number of times. If you need to iterate over a range of numbers or run a particular action repeatedly, the for loop can be useful.

It consists of three statements separated by semicolons, enclosed in parentheses, and followed by a code block.

The three statements are:

StatementsOverview
InitializationA statement that initializes the loop counter or any other variables used in the loop.
ConditionA boolean expression that is evaluated at the beginning of each loop iteration. If the expression is true, the loop continues to execute. If it is false, the loop terminates.
Increment/DecrementA statement that increments or decrements the loop counter or any other variables used in the loop.

Syntax

for (initialization; condition; increment/decrement)
{
//The code that needs to be executed
//for each iteration;
}

Now let’s take a closer look at each statement.

For loop Initialization Statement

Before the loop starts, the initialization statement is only executed once.

It initializes the loop counter as well as any other variables used in the loop.

Using a loop counter, for example, you can set other variables to 0 as needed:

Below is an example of displaying the numbers from 0 to 5 using the following code:

PHP for Loop Example: 

<?php for ($a = 0; $a <= 5; $a++) { echo "$a<br>" ; } ?>

Example Explained

  • $a = 0; – Set the loop counter ($a) to 0.
  • $a <= 5; – Loop until $a is less than or equal to 5
  • $a++ – Each iteration increases the loop counter by 1

For Loop Condition Statement

Each loop iteration starts with an evaluation of the condition statement.

The loop continues if the expression is true. Upon failure, the loop terminates.

To specify how many times the loop should run, you can use a condition.

Here is an example of counting by Two up to 20:

Example: 

<?php for ($a = 0; $a <= 20; $a+=1) { echo "$a<br>"; } ?>

Example Explained

  • $a = 0; – Set the loop counter ($a) to 0.
  • $a <= 5; – Loop until $a is less than or equal to 20.
  • $a++ – Each iteration increases the loop counter by 2.

For Loop Increment/Decrement Statement

Each loop iteration ends with an increment/decrement statement.

It increments or decrements the loop counter and any other variables used in the loop.

In each iteration of the loop, you can increase the counter by 5:

Example: 

<?php for ($a = 0; $a <= 20; $a+=5) { echo "$a<br>"; } ?>

The PHP for loop is a versatile construct that can be used in many different contexts.

You can use the for loop to iterate through an array and perform an operation on each element.

By using for loops in the example below, we will be able to display each data of an array:

Example: 

<?php $pets = array("Dog", "Cat", "Rabbit", "Mice", "Parrot"); //array contains pets data $arrlength = count($pets); // save lenght of an array for($a=0; $a < $arrlength; $a++){ echo $pets[$a]; //printing each pets iteratively echo "<br>"; //newline } ?>

Example Explanation

In above example we have created an array of pet names, then use a for loop to iterate through the array and print each pet name on a new line.

The length of the array is calculated by the “count” function and saved in a variable called $arrlength.

The loop runs for the length of the array and uses the loop variable $a to access each element of the array by its index, which is also $a.

The pet name is printed by the “echo” function, and a line break is added after each name with the HTML tag <br>.

You don’t have to worry about the array and count function as you will learn this in later posts. This is just an example of printing arrays through PHP for loops.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *