PHP Loop: While, Do While, For and Foreach Loops

What is PHP Loop

PHP loop is the process of executing the same block of code again and again until the specified condition is true. Loops are the best part of the PHP programming to perform a task many times you want to execute.

Suppose you want to echo a statement 20 times and you don’t know about PHP loops. You start typing the same statement 20 times. This takes time and more human effort. Now, to save this extra effort, you can use Loops with the condition that perform the repetitive task for 20 times.

Types of PHP loop

There are four types of loops in PHP. You can use these tasks to perform repetitive tasks:-

  • PHP while:- While condition executes a block of code again and again until the specified condition is true.
  • PHP do…while:- It first executes the block of code and then after executes this block of code again and again until the specified condition is true.
  • PHP for:- For condition executes a block of code at a specified numb of times.
  • PHP foreach: The loop can be used to iterate through each element of an array.

PHP while loop

PHP while loop executes a block of code again and again until the given condition is true. The loop first test the condition if it is true and if the condition is true, it executes the code and returns back to check the condition if it is true.

The process repeats itself and executes the code. If the condition is false, the loop stops executing the code and stop the loop also.

Syntax of PHP while loop

Example of PHP while loop
The example prints the number from 0 to 9.

Output

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9

PHP do…while loop

PHP do…while loop first executes a block of code and then after checks the given condition is true. The code will execute for the second time only if the given condition is true and the loop repeats itself again and again until the condition is true.

Syntax of PHP do…while loop

Example of PHP while loop
The example prints the number from 0 to 9.

Output

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9

PHP for loop

PHP for loop repeats the execution of a block of code for a certain number of times you specified. You can use this loop when you have an idea about how many times you to run the script again and again.

Syntax of PHP forloop

Example of PHP for loop
The example prints the number from 0 to 9.

Output

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9

PHP foreach loop

PHP foreach loop can be used to iterate through each element of an array. Foreach loop works only with arrays. You can print each element of an array using PHP echo statement.

Syntax of PHP foreach loop

Example of PHP foreach loop
The example prints the number from 0 to 9.

Output

Sachin
Virat
Dhoni

To see PHP foreach loop in more detail you can view our PHP foreach tutorial page.

Resources