Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

PHP for Loop


A for loop can be employed to determine in advance the number of times your script will run. PHP for loop can be very helpful when we want to loop over an array and then refer to a member of array by changing index. Suppose we have an array of odd numbers. To print them it is necessary to identify each item separately.


for (init counter for (test counter, init counter; increment counter) {for (init counter; test counter; increment counter)

Code to be executed each repetition;


The initializer can be used to determine the starting value of the counter that will be used to count the loop’s number of iterations. Variables can be used to accomplish this, and it is common to call it $i.

In the first for loop, the line is a description of three components. condition for statement is examined throughout the loop. The loop will stop in the event that this condition is not met. This happens when the iterator variable $i is found to be greater than the size of the array.

The increment statement is executed each time for increasing the index value by the desired amount. Most often, we will increment $i by 1. There are two ways to increase a variable’s value by 1. You can make use of $i+=1 or $i++ too.


Init counter: Initialize the loop counter’s value

Test counter: Analyzed every loop iteration. If it’s TRUE then the loop continues. If it’s a false, the loop ends then the loop ceases.

Increment Counters: Increases loop counter’s value

How Does PHP For Loop Work.

The loop starts to be evaluated at the time that the loop begins. The condition is evaluated one time every time. In the event that the test is valid the expression in the body will be executed. Otherwise, the loop ceases.

An increment expression will be evaluated one time at the end of each cycle. PHP lets you specify multiple expressions within the condition, start and increment of the for expression.

Additionally, you may also leave start, condition and increment blank, which indicates that PHP is not required to do anything in this phase.


The following example has five loops and alters the value assigned to two variables at each step of the loop.


$a = 0;

$b = 0;

For( the $i value is 0 and $i5 the $i++ ) {for( $i = 0; $i5; $i++)

$a += 10;

$b += 5;


echo (“At the end of the loop , a = $a, and b =” );


This will result in the next outcome –

At the close of the loop, A = 50 and B = 25

PHP Nested For Loop

For loops can be used inside for loops in PHP It is referred to as a nested for loop. Inside the for loop runs in conjunction with the outer loop’s conditions are met.

If inner loop is executed completely for one outer loop and outer for loop needs to be executed three times, and the inner for loop is to be executed three times, the inner for loop would be run nine times. (3 times for the first outer loop, three times for the 2nd outer loop, and three times for the third outer loop).




echo “$i $j






1 1

1 2

1 3

2 1

2 2

2 3

3 1

3 2

3 3

PHP While loop

They run a code block in a series of times until the set condition is fulfilled While loops are utilized to execute the code block until a particular condition comes real.

PHP Loop Flowchart

It is possible to use an in loop for reading data that are returned by a database query.

Types of while loops

Do while is executed at a minimum before taking a look at the situation

While Tests for the conditions first. If the condition is true the code block is executed for as while the situation remains valid. If it is false then the execution of the while loop ends.

Syntax of While Loop


while (condition){

block of code to be executed;



Foreach loop

Each loop of PHP is utilized to traverse array elements. The foreach loop traverses an element that is iterable, such as an array or object, and provides the members of a particular array one at a.


foreach (array as value) {foreach (array as value)

code to be executed


Example to Show the Array’s Values

“$array” = array( 1 2 3 4 5,);

foreach( $array as value ) {foreach( $array as $value)

echo “Value is $value” echo “Value




The next result:

Value is 1

Value is 2

Value is 3

Value is 4

Value is 5

Categorized as PHP

Leave a comment

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