Syntax:
for (Initialization; Termination_Condition; Increment/Decrement)
{
//Statements….
}
The “for” in PHP contains three parts like other standard programming languages. The use of each part of the loop is explained in the following:
- Initialization
The first part contains the initialization value or values that is used to start the loop. This part is optional. - Termination_Contition
The second part contains the termination condition of the loop that is used to terminate the loop. This part is also optional. - Increment/Decrement
The last part of the loop contains the increment or decrement value of the counter in each iteration that is used in the initialization part of the loop. The last part of the loop is also optional.
Different Uses of the “For” Loop
The different uses of the “for” loop is shown in this part of the tutorial using multiple examples.
Example 1: Iterating the List of Numbers
Create the PHP file with the following script that iterates the loop 15 times and prints all even numbers within 1 to 15:
echo "All even numbers within 1 to 15<br />";
//Iterate the loop 15 times
for($n=1; $n<=15; $n++)
{
//Check if the number is even or not
if($n%2 == 0)
echo $n, " ";
}
?>
Output:
The following output appears after executing the previous script:
Example 2: Defining Infinite “For” Loop
Create a PHP file with the following script that prints the number from 10 to 6 using an infinite “for” loop. Here, the break statement is used to terminate the loop:
//Initialize the counter variable
$counter = 10;
//Declare infinite loop
for (;;)
{
//Check the counter value
if ($counter < 6)
break;
else
echo 'The current value of $counter is ', $counter, "<br />";
//Decrement the counter value
$counter--;
}
?>
Output:
The following output appears after executing the previous script:
Example 3: Reading the Values of a Numeric Array
Create a PHP file with the following script that prints the values of a numeric array that contains four values. The count() function is used to count the total number of elements of the array and the loop is iterated based on the output of the count() function.
Output:
The following output appears after executing the previous script:
Example 4: Reading the Values of an Associative Array
The “foreach” loop is mainly used to read the values of the associative array. But the “for” loop can be used to read the values of the associative array using the count() function. Create a PHP file with the following script that iterates and prints the values of a two-dimensional array using the “for” loop:
//Define an associative array
$students = array(
array('name' => "Nira Hossain", 'marks' => 85),
array('name' => "Janifer Ahmed", 'marks' => 78),
array('name' => "Maruf Chowdhury", 'marks' =>91)
);
//Iterate the loop to read array values and keys
for($in = 0; $in < count($students); $in++ )
{
echo $students[$in]['name']." obtained ".$students[$in]['marks']." marks.<br />";
}
?>
Output:
The following output appears after executing the previous script:
Example 5: Iterate the Range of Dates
The range of date values can be generated using the strtotime() function and the “for” loop. The strtotime() function is used to generate a timestamp value based on the date value. The date() function is used to generate a formatted date value. Create a PHP file with the following script that generates 7 dates using strtotime() and date() functions. The “for” loop is iterated 7 times based on the starting and ending timestamp values and prints the formatted date values.
//Set the starting date
$start_date = '2022-12-01';
//Set the ending date
$end_date = '2022-12-07';
//Iterate the date values
for ($dt = strtotime($start_date); $dt
Output:
The following output appears after executing the previous script:
Example 6: Read the Content of a File
The “for” loop can be used to print the content of the file. Create a text file named country.txt with the following content that is used in this example:
Bangladesh
Japan
Germany
Norway
Create a PHP file with the following script that prints the content of a text file using the “for” loop. The file_get_contents() function is used in this script to read the full content of a file in a string variable. The explode() function is used to create an array by splitting the file content based on the “\n” character. Next, the “for” loop is used to print the array values that contain each line of the file in each array element.
//Read the file content into a string
$fh = file_get_contents("country.txt");
//Create an array based on the newline of the file
$file_array = explode("\n",$fh);
//Iterate the array to print the file content
for ($i = 0; $i < count($file_array); $i++)
{
echo $file_array[$i], "<br/>";
}
?>
Output:
The following output appears after executing the previous script:
Conclusion
The “for” loop is one of the useful loops of PHP to solve the different types of programming problems. The uses of the “for” loop for different purposes are explained in this tutorial using simple examples to help the new PHP users.