For Loop

The for loop - Loops through a block of code a specified number of times.It means for statement is used when you know how many times you want to execute a statement or a block of statements.



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



  • Initialization Expression: In this expression we have to initialize the loop counter to some value. for example: $num = 1;
  • Test Expression (Condition): In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of loop and go to update expression otherwise we will exit from the for loop. For example: $num <= 10;
  • Update Expression (Increment / decrement): After executing loop body this expression increments/decrements the loop variable by some value. for example: $num += 2;



for ($x = 0; $x <= 10; $x++) 
  echo "The number is: $x <br>";



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
The number is: 10


Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 10; - Continue the loop as long as $x is less than or equal to 10
  • $x++ - Increase the loop counter value by 1 for each iteration