> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP For Loop

A `for` loop in PHP is a definite iteration control structure that executes a block of code repeatedly based on a boolean condition. It consolidates the initialization, condition evaluation, and iteration update into a single, compact statement.

## Syntax

PHP supports both standard curly-brace syntax and an alternative colon-based syntax for template files.

**Standard Syntax:**

```php theme={"dark"}
for (initialization; condition; iteration) {
    // Statements to execute
}
```

**Alternative Syntax:**

```php theme={"dark"}
for (initialization; condition; iteration):
    // Statements to execute
endfor;
```

## Component Breakdown

The `for` statement is divided into three distinct expressions, separated by semicolons. All three expressions are optional.

1. **Initialization (`expr1`)**:
   * Executed unconditionally exactly once before the loop begins.
   * *Note:* Variables initialized here are not block-scoped to the loop; they remain accessible in the surrounding scope after the loop terminates.
2. **Condition (`expr2`)**:
   * Evaluated at the beginning of every iteration.
   * If it evaluates to `true`, the loop body executes.
   * If it evaluates to `false`, the loop terminates immediately, and execution continues with the code following the loop.
   * If omitted, it implicitly evaluates to `true`, resulting in an infinite loop unless terminated by control flow statements such as `break`, `return`, `exit`, `die`, `goto`, or by throwing an `Exception`.
3. **Iteration (`expr3`)**:
   * Executed at the end of every iteration.
   * This expression is guaranteed to execute after the loop body, even if the current iteration is aborted early via a `continue` statement. This guaranteed execution is a primary mechanical distinction between `for` and `while` loops.

## Execution Flow and Control Structures

The internal execution sequence of a `for` loop dictates exactly when expressions are evaluated and how control structures interact with them:

1. The `initialization` expression is executed.
2. The `condition` expression is evaluated.
3. If `condition` is `false`, the loop terminates. If `true`, the loop body executes.
4. If a `continue` statement is encountered within the body, the remainder of the body is skipped, and control immediately jumps to Step 5.
5. The `iteration` expression is executed.
6. Control returns to Step 2.

## Advanced Mechanics

**Multiple Expressions via Comma Operator**
PHP allows multiple expressions within any of the three parameters by separating them with commas.

* In the **initialization** and **iteration** parameters, all comma-separated expressions are executed from left to right.
* In the **condition** parameter, all comma-separated expressions are evaluated, but the loop's continuation depends *strictly on the boolean evaluation of the final expression*.

```php theme={"dark"}
// Multiple initializations and iterations
for ($i = 0, $j = 10; $i < 10; $i++, $j--) {
    // Loop body
}
```

**Omitted Expressions**
Any or all of the expressions can be left empty. The semicolons, however, are syntactically required.

```php theme={"dark"}
$i = 0;
// Initialization and iteration omitted from the declaration
for (; $i < 10;) {
    $i++;
}

// Infinite loop (all expressions omitted)
for (;;) {
    // Requires internal control flow (e.g., break, return, exit) to terminate
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
