codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

10 / 56 topics
7Control Structures8If, Else, and Elseif Statements9Switch Case Statement10For Loop11While Loop12Do While Loop13Foreach Loop
Tutorials/PHP/For Loop
🐘PHP

For Loop

Updated 2026-05-15
10 min read

For Loop

Introduction

In PHP, loops are used to execute a block of code repeatedly. One of the most commonly used loops is the for loop. This tutorial will guide you through understanding how to use the for loop in PHP, including its syntax and practical examples.

Concept

The for loop in PHP allows you to repeat a block of code a specific number of times. It consists of three parts: initialization, condition, and increment/decrement. The basic syntax of a for loop is as follows:

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

- **Initialization**: This part is executed only once before the loop starts. It is typically used to initialize one or more loop control variables.
  
- **Condition**: Before each iteration of the loop, this condition is evaluated. If it evaluates to `true`, the loop body is executed. If it evaluates to `false`, the loop terminates.

- **Increment/Decrement**: This part is executed after each iteration of the loop. It is usually used to update the loop control variable(s).

## Examples

### Example 1: Basic For Loop

Let's start with a simple example where we print numbers from 1 to 5 using a `for` loop.

```php
<CodeBlock language="php">
{`<?php
for ($i = 1; $i <= 5; $i++) {
    echo "Number: " . $i . "\n";
}
?>`}
</CodeBlock>

When you run this code, the output will be:

<OutputBlock>
{`Number: 1
Number: 2
Number: 3
Number: 4
Number: 5`}
</OutputBlock>

### Example 2: Using For Loop with Arrays

You can also use a `for` loop to iterate over an array. Here's an example where we print each element of an array.

```php
<CodeBlock language="php">
{`<?php
$fruits = ["Apple", "Banana", "Cherry"];
for ($i = 0; $i < count($fruits); $i++) {
    echo "Fruit: " . $fruits[$i] . "\n";
}
?>`}
</CodeBlock>

The output will be:

<OutputBlock>
{`Fruit: Apple
Fruit: Banana
Fruit: Cherry`}
</OutputBlock>

### Example 3: Infinite For Loop

Be cautious when using `for` loops, as you can create infinite loops if the condition never becomes false. Here's an example of an infinite loop:

```php
<CodeBlock language="php">
{`<?php
for ($i = 1; ; $i++) {
    echo "This is iteration number: " . $i . "\n";
}
?>`}
</CodeBlock>

To stop this loop, you would need to manually interrupt the execution (e.g., by pressing Ctrl+C in the terminal).

## What's Next?

Now that you understand how to use `for` loops in PHP, you might want to explore other types of loops. The next topic will cover the `while` loop, which is another powerful tool for repeating code under certain conditions.

Stay tuned for more tutorials on control flow and advanced PHP concepts!

<Tip variant="info">
Remember, practice makes perfect! Try creating your own examples using `for` loops to reinforce what you've learned.
</Tip>

PreviousSwitch Case StatementNext While Loop

Recommended Gear

Switch Case StatementWhile Loop