PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Looping Statements in PHP

Looping statements in PHP allow you to execute a block of code repeatedly based on certain conditions. Here’s a deep dive into the different types of loops available in PHP:

1. 'for' Loop

The ‘for‘ loop is used when you know in advance how many times you want to execute a statement or a block of statements.

Syntax:

				
					for (initialization; condition; increment) {
    // code to be executed
}

				
			
  • Initialization: Executed once, sets the starting point.
  • Condition: Evaluated before each iteration. If true, the loop continues; if false, the loop stops.
  • Increment/Decrement: Executed at the end of each iteration.

Example:

				
					for ($i = 0; $i < 10; $i++) {
    echo $i;
}

				
			

Explanation:

  • Initializes ‘$i‘ to ‘0‘.
  • Checks if ‘$i‘ is less than ‘10‘. If true, executes the loop.
  • Increments ‘$i‘ by ‘1‘ after each iteration.

2. while Loop

The ‘while‘ loop is used when you want to execute a block of code repeatedly as long as the condition is true.

Syntax:

				
					while (condition) {
    // code to be executed
}

				
			

Example:

				
					$i = 0;
while ($i < 10) {
    echo $i;
    $i++;
}

				
			

Explanation:

  • Checks if ‘$i‘ is less than ‘10‘. If true, executes the loop.
  • Increments ‘$i‘ by ‘1‘ after each iteration.

3. do-while Loop

The ‘do-while‘ loop is similar to the ‘while‘ loop, but the condition is checked after the code block has been executed. This means the block of code will execute at least once.

Syntax:

				
					do {
    // code to be executed
} while (condition);

				
			

Example:

				
					$i = 0;
do {
    echo $i;
    $i++;
} while ($i < 10);

				
			

Explanation:

  • Executes the block of code.
  • Checks if ‘$i‘ is less than ‘10‘. If true, repeats the loop.

4. 'foreach' Loop

The ‘foreach‘ loop is used to iterate over arrays. It works only on arrays and is used to loop through each key/value pair in an array.

Syntax:

				
					foreach ($array as $value) {
    // code to be executed
}

				
			

or

				
					foreach ($array as $key => $value) {
    // code to be executed
}

				
			

Example:

				
					$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $color) {
    echo $color;
}

				
			

Explanation:

  • Iterates over each value in the ‘$colors‘ array and assigns it to ‘$color‘.
  • Executes the block of code for each value in the array.

Example with keys:

				
					$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");

foreach ($age as $name => $age) {
    echo "$name is $age years old.";
}

				
			

Explanation:

  • Iterates over each key/value pair in the ‘$age‘ array.
  • Assigns the key to ‘$name‘ and the value to $age.
  • Executes the block of code for each key/value pair.

Looping statements are essential for performing repetitive tasks efficiently in PHP. Understanding how to use ‘for‘, ‘while‘, ‘do-while‘, and ‘foreach‘ loops will enable you to handle different looping scenarios effectively.

Scroll to Top