Skip to main content

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.

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:
for (initialization; condition; iteration) {
    // Statements to execute
}
Alternative Syntax:
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.
// 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.
$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
}
Master PHP with Deep Grasping Methodology!Learn More