PHP Tutorials: Loops

In this tutorial, we are going to learn about loops. Many beginner and experienced developer fail to understand the basic concept of loops. Loop is one of the most important element to become a good programmer. Before we move ahead with definition and syntax we should first know what exactly a loop is. A loop is a repetitive statement which gets executed until the condition is true. Let us take an example.

<?php
	echo "This is statement number 1<br>";
	echo "This is statement number 2<br>";
	echo "This is statement number 3<br>";
	echo "This is statement number 4<br>";
	echo "This is statement number 5<br>";
	echo "This is statement number 6<br>";
	echo "This is statement number 7<br>";
?>

In the above example, we have written a single statement with a minor change in it. If you calculate the efforts to write 7 lines of code is too much. Loop is a good solution to this problem. With Loops, you can execute a single set of a statement with a specific condition.

PHP has 4 Loop statements.

  1. while – It also called pre-test loop. The statement keeps executing until the condition is true.
  2. do-while: It also called a post-test loop. The loop will execute at least once even if the condition is false.
  3. for: The functioning of this loop is very similar to while loop. There is a difference in syntax.
  4. foreach: This loop is used specifically to traverse every element of an array.

while loop

The while loop executes a block of the statement until the condition specified in while loop is true. It is also called Pre-test loop because it tests the condition before it executes the block of the statement.

Syntax:
<?php
	while (condition) {
		Statement 1;
		Statement 2;
	}
?>

Let us take an example of a while loop. I am going to solve the problem which we discussed at the starting of this tutorial.

<?php
	$i = 1; //Assigment 
	while ( $i <= 7 ) { // condition
		echo "This is statement number $i<br>"; //echo statement with value of $i
		$i++; //post increment
	}
?>
Output: 
This is statement number 1
This is statement number 2
This is statement number 3
This is statement number 4
This is statement number 5
This is statement number 6
This is statement number 7

What this while loop is doing actually?

  1. I have taken a variable $i and assigned 1 into it, this is done to set an initial value to start with.
  2. I added a condition $i <= 7 (comparison operator). The loop will execute until the value of $i is less than or equal to 7. That means the loop will execute 7 times.
  3. Inside the loop, I have added an echo statement, that gets executed every time the control gets inside the statement block.
  4. After execution, there is a Post-increment statement which will increase the value of $i by 1.

Hope you got an idea of while loop.

do-while loop

do-while is also called Post-test loop because this loop checks the condition after the execution of the statement block. One another fact about do-while is, it executes the statement at least once even if the condition is false. It also uses the comparison operator.

Syntax:
<?php
	do {
		Statement 1;
		Statement 2;
		...
		...
		Statement n;
	} while ( condition );

?>

Let us take an example. In this example, I will take the same problem which we took in while loop. There is a specific reason to take this example. You can compare both and see the difference.

<?php
	$i = 1; 
	do {
		echo "This is statement number $i<br>";
		$i++; //post increment
	} while ( $i <= 7 );
?>
Output: 
This is statement number 1
This is statement number 2
This is statement number 3
This is statement number 4
This is statement number 5
This is statement number 6
This is statement number 7

for loop

One basic thing to understand about the loop is the way it works. Every loop statement is similar is one or another way and there is a difference of syntax or some minor execution time.

for loop is also similar to while loop but the difference is of syntax and the way we write it. In while loop, we initialize the variable before while statement, re-assign new value in-between the statement block and condition is with while statement. But in for loop, we do everything in a single line.

Syntax:
<?php
	for (assigment; condition ; re-assignment) { 
		Statement 1;
		Statement 1;
		...
		...
		Statement n;
	}
?>
Fragments of for loop
  1. assignment: A variable to use as a counter. The variable can be initialized here as well.
  2. condition: Condition to check for execution.
  3. re-assignment: It updates the loop counter with the new value.
<?php
	for ($i=1; $i <= 7; $i++) { 
		echo "This is statement number $i<br>";
	}
?>
Output:
This is statement number 1
This is statement number 2
This is statement number 3
This is statement number 4
This is statement number 5
This is statement number 6
This is statement number 7

foreach loop

foreach loop is best suitable for traversing an array or an object. We will talk about object laters. foreach loop helps to check through all the elements of an indexed array, associative array, and a multidimensional array.

Syntax:
<?php
	foreach ($variable as $key => $value) {
		# code...
	}
?>
Fragments of foreach loop
  1. $variable: The array variable that needs to be iterated.
  2. $key: It can be numeric, alphanumeric and alphabetic
  3. $value: Value at the current position.

Let us take an example.

<?php
	$cars = array( 'BMW', 'Audi', 'Hyundai', 'Honda' );
	foreach ($cars as $key => $value) {
		echo "Value at position $key is $value<br>";
	}
?>
Output:
Value at position 0 is BMW
Value at position 1 is Audi
Value at position 2 is Hyundai
Value at position 3 is Honda

In the output, you can see that the $key has a numeric value and it starts from 0.

Let us take an example of an associative array

<?php
	$cars = array( 
		'first' => 'BMW', 
		'second' => 'Audi', 
		'third' => 'Hyundai', 
		'fourth' => 'Honda' 
	);
	foreach ($cars as $key => $value) {
		echo "Value at position $key is $value<br>";
	}
?>
Output: 
Value at position first is BMW
Value at position second is Audi
Value at position third is Hyundai
Value at position fourth is Honda

Leave a Reply

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