PHP For Loop

Introduction

One of the most commonly used loops in PHP is the for loop. It is especially useful when you know in advance how many times you want to execute a block of code.

The PHP for loop is simple, powerful, and widely used in real-world applications.

What is a PHP For Loop?

The for loop in PHP is used to execute a block of code repeatedly for a specific number of times.

It consists of three main parts:

  1. Initialization
  2. Condition
  3. Increment/Decrement

Note: The loop runs until the condition becomes false.

Syntax

Basic Syntax:


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

Explanation:

  • Initialization → sets the starting value
  • Condition → checks whether the loop should continue
  • Increment/Decrement → updates the loop variable

Examples:

Example 1: Simple Increment Loop


<?php
for ($x = 1; $x < 5; $x++) {
   print $x . "<br/>";
}
?>

Output:

1
2
3
4

Example 2: Decrement Loop


<?php
for ($x = 5; $x > 1; $x--) {
   print $x . "<br/>";
}
?>

Output:

5
4
3
2

Example 3: Printing Even Numbers


<?php
for ($i = 2; $i <= 10; $i += 2) {
   echo $i . "<br/>";
}
?>

Example 4: Loop with Multiple Variables


<?php
for ($i = 1, $j = 10; $i <= 5; $i++, $j--) {
   echo "i = $i, j = $j <br/>";
}
?>

Example 5: Nested For Loop


<?php
for ($i = 1; $i <= 3; $i++) {
   for ($j = 1; $j <= 2; $j++) {
       echo "i = $i, j = $j <br/>";
   }
}
?>

Real-Life Example

Scenario1: Multiplication Table


<?php
$num = 5;
for ($i = 1; $i <= 10; $i++) {
   echo "$num x $i = " . ($num * $i) . "<br/>";
}
?>

Why this is useful:

  • Common in educational apps
  • Helps automate calculations
  • Reduces manual work

Scenario 2: Display List of Products


<?php
$products = ["Laptop", "Mobile", "Tablet"];
for ($i = 0; $i < count($products); $i++) {
   echo $products[$i] . "<br/>";
}
?>

Note: Used in e-commerce websites to display items dynamically.

Common Mistakes

1. Infinite Loop


for ($i = 1; $i <= 5; $i--) {
   echo $i;
}

Note: The condition never becomes false.

2. Wrong Condition


for ($i = 1; $i > 5; $i++) {

Note: The loop will never execute.

3. Missing Increment/Decrement


for ($i = 1; $i <= 5;) {

Note: Causes an infinite loop.

4. Using Wrong Comparison Operator

Be careful with <, <=, >, >=.

5. Not Using count() Properly


for ($i = 0; $i <= count($arr); $i++)

Note: This may cause an undefined index error.

Conclusion

The PHP for loop is a powerful and flexible tool for handling repetitive tasks. It provides a clean and structured way to control iterations, making your code more efficient and readable.

PHP for loop – Interview Questions

Q 1: What is a for loop in PHP?
Ans: It repeats code a fixed number of times.
Q 2: Syntax of for loop?
Ans: for ($i=0; $i<5; $i++) { }
Q 3: When should for loop be used?
Ans: When iteration count is known.
Q 4: Can for loops be nested?
Ans: Yes.
Q 5: Is the loop counter mandatory?
Ans: No, but recommended.

PHP for loop – Objective Questions (MCQs)

Q1. for loop is ideal when the number of iterations is ______.






Q2. How many parts are in a for loop declaration?






Q3. Which part of the for loop executes only once?






Q4. Which keyword exits a loop immediately?






Q5. for loop executes the body ______ times when the condition is true.






Related PHP Tutorials