🔁 PHP Control Flow & Logic
Estimated reading: 3 minutes 116 views

🔂 PHP For Loop – Syntax, Examples, and Real-World Uses

A detailed guide on how to use the for loop in PHP for fixed-count iterations.


🧲 Introduction – Why Use the for Loop?

The for loop is ideal when you need to run code a known number of times. It’s perfect for counting, iterating through indexed arrays, and generating patterns or sequences. With initialization, condition, and increment in one line, it’s one of PHP’s most efficient looping structures.

🎯 In this guide, you’ll learn:

  • for loop syntax and structure
  • How each part of the loop works
  • Practical examples and variations
  • Loop control using break and continue
  • Best practices and performance tips

✅ Basic Syntax of for Loop

for (initialization; condition; increment) {
    // code to execute
}

Example:

for ($i = 1; $i <= 3; $i++) {
    echo "Iteration: $i <br>";
}

🔍 Output:

Iteration: 1
Iteration: 2
Iteration: 3

📘 The loop:

  • Starts at $i = 1
  • Continues while $i <= 3
  • Increments $i after each run

🔁 Reverse Looping

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

🔍 Output: 5 4 3 2 1

📘 Decrementing instead of incrementing. Useful for countdowns.


🔄 Skipping Values

for ($i = 0; $i <= 10; $i += 2) {
    echo "$i ";
}

🔍 Output: 0 2 4 6 8 10

📘 Incrementing by 2 skips odd numbers — efficient for step-based loops.


🧪 Loop With Condition Only (Initialization Outside)

$i = 1;

for (; $i <= 3; $i++) {
    echo "Run: $i<br>";
}

📘 You can skip initialization inside the loop if done beforehand.


🧠 Controlling Loop Execution

🚫 break to Exit Early

for ($i = 1; $i <= 5; $i++) {
    if ($i == 4) break;
    echo "$i ";
}

🔍 Output: 1 2 3

📘 Loop stops when $i equals 4.


🔂 continue to Skip Iteration

for ($i = 1; $i <= 5; $i++) {
    if ($i == 3) continue;
    echo "$i ";
}

🔍 Output: 1 2 4 5

📘 Skips iteration when $i is 3.


💡 Real-World Example – Generating a Dropdown

echo "<select>";
for ($year = 2020; $year <= 2025; $year++) {
    echo "<option>$year</option>";
}
echo "</select>";

📘 Useful for generating year, date, or pagination lists dynamically.


🧠 Best Practices

  • ✅ Use for when iteration count is known
  • ✅ Keep loop logic minimal for performance
  • ✅ Avoid modifying the counter ($i) inside the loop
  • ✅ Use descriptive variable names ($index, $counter) in complex loops

📌 Summary – Recap & Next Steps

The for loop is compact, flexible, and best used when you know exactly how many times to repeat a task.

🔍 Key Takeaways:

  • Use for to iterate with a counter
  • Ideal for step-based and numeric iterations
  • Control loops with break and continue

⚙️ Real-World Use Cases:
Pagination, number generators, form fields, date pickers, and iteration over indexes.


❓ Frequently Asked Questions (FAQs)

❓ Can I omit initialization or increment in a for loop?
✅ Yes. You can write: for (; condition ;) and manage variables outside the loop.

❓ What’s the difference between for and while?
for is best when the number of iterations is known; while is better for unknown loops.

❓ Can I nest for loops?
✅ Yes, commonly used in matrix and table generation:

for ($i = 1; $i <= 3; $i++) {
    for ($j = 1; $j <= 3; $j++) {
        echo "($i, $j) ";
    }
}

Share Now :
Share

🔂 PHP For Loop

Or Copy Link

CONTENTS
Scroll to Top