elibraryportal Logo

Loops Statement in PHP

PHP loops Statements are used to execute the same block of code again and again.

Types of Loops in PHP

PHP supports 4 different types of loops.

  1. for
  2. while
  3. do...while
  4. foreach

1) For Loop in PHP

for loop repeats a block of code until a certain condition is that means execute a block of code for certain number of times.

Syntax

Flowchart

ForLoop

Example :-

Output

Number is: 0 
Number is: 1 
Number is: 2 
Number is: 3 
Number is: 4 
Number is: 5 

2) While Loop in php

if the while loop condition is true then executes a block. Other wise sikp it.

Syntax

PHP While Loop Flowchart

WhileLoop

Example :-

Output

The number is: 1 
The number is: 2 
The number is: 3 
The number is: 4 
The number is: 5
 

3) do...while Loop in PHP

do...while loop will always execute the block of code once it will check the condition, and repeat the loop while the specified condition is true.

Syntax:

Output

Example :-

4) foreach Loop in PHP

only works on arrays and is used to loop through each key/value pair in an array.

Syntax

Output

java 
php 
html 
css 
 

Next Topic