PHP For Loop

Loops in PHP are used to perform a task repeatedly. PHP for loop can be used to traverse set of code for the specified number of times. It is typically used to execute a block of code for certain number of times.

Generally for loop is used when you know in advance how many times the script should run.


for(initialization; condition; increment/decrement) {
///statements to be executed
  • initialization – Initialize the loop counter value. it holds an integer value.
  • condition – Evaluate each iteration value. The condition is checked at each iteration. The loop continuously executes until the condition is false. If TRUE, the loop execution continues, otherwise the execution of the loop ends.
  • Increment/decrement – It increments or decrements the value of the variable.


Flowchart of for loop in PHP


for ($x = 0; $x <= 10; $x++) {
  echo "The number is: $x <br>";
Output of for loop in PHP example