For Loop in PHP

WBOY
Release: 2024-08-29 12:41:11
Original
1131 people have browsed it

Loops in PHP are used to perform a task repeatedly. For Loop in PHP has various forms. For loop loops a number of times like any other loop, ex. While loop. While loop and for loop executes a block of code, which is based on a condition. When it is known beforehand that a particular block of code should execute this number of times, say 5 times we use for loop. Whereas while Loop is used as long as the condition mentioned satisfies. Similar is for do-while loop, when we want do not know how many times the loop should be executed but know that it should execute at least once, then we use the do-while loop. Similarly other loops and so on.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

For loop contains different expressions. These expressions can be initialization, it can be a condition, etc.

A for loop contains expressions followed by a semicolon, the following is the syntax.

Syntax

for(initialization; condition; increment/decrement) {
///statements to be executed
}
Copy after login

Where: for loop is the block which contains the code

Initialization: is the declaring and assigning or to initialize the value of  variable used, it holds an integer value

Condition: for the loop to work this condition is checked first and evaluated if it is true only then the loop executes further.

Increment/Decrement: this increases/decreases the value of the variable for the loop to iterate.

Flowchart

For Loop in PHP

How For Loop Works?

First, the initialization of variables is evaluated. Second, with each iteration of loop condition is checked, if it is true, the execution will continue, and the block of code or statements will execute. And if the condition checked is not true, meaning false then the loop ends, there itself without the block of code or statements to be executed. Last the increment and decrement on the initialized variable are done after the execution of the statements mentioned.

Examples of For Loop in PHP

Here are the following examples mention below

Example #1

Remember in the below program, the value of i is initialized to 0, therefore, the $i variable is printed using echo, we get the values beginning with 0 and continue to print till 5 as the condition is to print till value 5.

<?php
//example to demonstrate simple for loop
for($i=0; $i<=5;$i++) {             // declaring variable i, condition , incre/decr
echo '<br>';                                        // line break
echo 'Value of i is '. $i;  //printing the value of variable i
}
?>
Copy after login

Output :

For Loop in PHP

Example #2

In the below program, the value of i is initialized to 1, therefore, the $i variable is printed using echo statement, we get the values beginning with 0 and continue to print till 5 as the condition is to print till value 5.

Here the initializing of variable I is not in the for loop but outside the for loop at the beginning of the program.

<?php
//example to demonstrate for loop
$i=1;                                                                 // declaring variable i
for(; $i<=5;$i++) {                                        //  condition , increment and decrement
echo '<br>';                                        // line break
echo 'Value of i is '.$i;                  // printing the value of variable i
}
?>
Copy after login

Output :

For Loop in PHP

Example #3

In this program, the expression containing the condition is not mentioned in the for loop statement but inside the for loop like if($i == 4) followed by a break statement.

While iterating if the $i value reaches the value 4 the control will jump out of the for loop .

<?php
//example to demonstrate for loop
for($i=1; ;$i++) {          // declaring variable i , increment and decrement
if($i == 4) {                             // condition
break;
}
echo '<br>';                                        // line break
echo 'Value of i is '.$i;  // printing the value of variable i
}
?>
Copy after login

Output:

For Loop in PHP

Example #4

In this program, the increment and decrement are mentioned inside the for a loop after printing the value of I variable to continue the iteration.

<?php
//example to demonstrate for loop
for($i=1;$i<=10;) {          // declaring variable i declaring  condition
if($i == 7) {
break;
}
echo '<br>';                                          // line break
echo 'Value of i is '.$i;                     // printing the value of variable i
$i++;                                                     // increment and decrement
}
?>
Copy after login

Output:

For Loop in PHP

Example #5

In this program, the for loop does not contain any expression but mentioned differently.

<?php
//example to demonstrate for loop
$i=1;                                                               // declaring variable i
for(;;) {
if($i == 8) {                                         // declaring  condition ,
break;
}
echo '<br>';                                        // line break
echo 'Value of i is '.$i;                     // printing the value of variable i
$i++;                                                   // increment and decrement
}
?>
Copy after login

Output :

For Loop in PHP

Example #6

In the following program, we use for loop to iterate an array. We can iterate using for and foreach loop both. Also, here the array used can be an array-like indexed array, associative array.

The fruit is an array, we count the length of the array using count function and get the length of the array, which is 4 so the for loop will iterate accordingly and print the name of the fruits.

<?php
//example to demonstrate for loop for array
$fruits = array('orange', 'banana', 'papaya', 'strawberry');
$count = count($fruits);
for($i=0; $i<$count; $i++) {
echo '<br>';
echo 'Fruit Name ==>'.$fruits[$i];
}
?>
Copy after login

Output :

For Loop in PHP

Example #7

In this program, for loop is used to print star pattern,

<?php
//example to demonstrate star pattern using for loop
for($i=0;$i<=5;$i++)
{
for($j=0;$j<=$i;$j++) {
echo " * ";
}
echo "<br/>";
}
?>
Copy after login

Output:

For Loop in PHP

Example#8

In the following program, the foreach loop is used to iterate through a fruit loop.

<?php
//example to demonstrate array using foreach loop
$directions = array('east','west','north', 'south');
foreach($directions as $key=>$value) {
echo 'Direction =>'. $value.'<br/>';
}
?>
Copy after login

Output:

For Loop in PHP

Conclusion – For Loop in PHP

In this article, we learned about for loop, the syntax of the flow chart, how the loop works in PHP and related loops like the foreach loop. We also learned how the loop iterates normally and also how it iterates through arrays, we also learned how for loop is used to print the star pattern. Hope this article is found to be informative and useful.

The above is the detailed content of For Loop in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!