PHP Loop

PHP

Share

In this tutorial we will learn about different types of loop in PHP like while, do...while and for.

Why use loop?

When we want to run the same block of code again and again we take help of loop.

while loop

In this loop we first check if the expression evaluates to true. If it is true then the code inside the while block is executed. Then the expression is again tested. If it is true again then the code inside the while block is executed. If the expresion evaluates to false we exit the loop. Following is an example of while loop.

while ( expression ) {
	//some code
}

//some more code

Remember!
while loop - first check then execute.

Let us print 1 to 5 using while loop.

$x = 1;

while ( $x <= 5 ) {
	
	echo $x . " ";

	$x++;

}

Output:
1 2 3 4 5

In the above code we have first initialized x to 1. Then we are checking if the expression, is x less than or equal to 5.

If the expression $x <= 5 evaluates to true we move inside the while loop block.

Next we echo the value of x.

Finally, in the last line of the while block we use the increment operator to increase the value of x by 1.

The expression is again evaluated and the loop is executed till $x is less than or equal to 5. When the value of $x becomes 6, we exit the loop.

do...while loop

The do...while loop is similar to a while loop except it for one thing. Here we execute the code then we perform the check.

Remember!
do...while loop - first execute then check.

Following is a syntax for do...while loop.

do {
	//some code
} while ( expression );

//some more code

In the following example we will print from 1 to 5 using do...while loop.

$x = 1;

do {
	
	echo $x . " ";

	$x++;

} while ( $x < 6 );

Output:
1 2 3 4 5

In the above code, we first initialize x to 1 then we enter into the do...while loop.

Inside the loop we first echo the value of x. Then we increment x by 1 using the increment operator. And then we check if the value of x is less than 6.

We break out of the loop when the the expression $x < 6 evaluates to false.

for loop

The for loop is a more compact loop. We have the initializing, testing and updating expression all in one place. Following is a for loop syntax.

for ( initializer_expression; test_expression; update_expression ) {
	
	//some code

}

//some more code

First, the initializer_expression portion is executed. It is where we do initialization of variables. And this is visited only once.

The test_expression portion is where we perform checking. If it evaluates to true then the body of the for loop is executed, otherwise we exit the loop.

After the body of the for loop is executed we visit the update_expression portion. This is where we update any value that is used in the test_expression portion.

After the update_expression portion is executed we revisit the test_expression portion.

Let us understand the working of for loop by the following example in which we are printing from 1 to 5.

for ( $x = 1; $x <= 5; $x++ ) {
	echo $x . " ";
}

In the above code we are first initializing variable x to 1. Then we are checking is x less than or equal to 5. It is at this point. So, we move into the body of the for loop and print x. Then we increment the value of x by 1 using increment operator.

Now we again perform the check $x <= 5. If this is true we execute the for loop body one more time otherwise, we break out of the loop.