> ## 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 While Loop

The `while` loop in PHP is a pre-test control flow statement that repeatedly executes a block of code as long as a specified boolean condition evaluates to `true`. Because the condition is evaluated before each iteration, the loop body will execute zero times if the initial condition evaluates to `false`.

## Syntax

PHP supports two syntactical structures for the `while` loop: the standard curly-brace syntax and the alternative colon-based syntax (commonly used in templating).

**Standard Syntax:**

```php theme={"dark"}
while (expression) {
    // Statements to execute
}
```

**Alternative Syntax:**

```php theme={"dark"}
while (expression):
    // Statements to execute
endwhile;
```

## Execution Mechanics

1. **Expression Evaluation:** At the start of each iteration, PHP evaluates the `expression`.
2. **Type Casting:** PHP automatically casts the result of the `expression` to a boolean. Values considered "truthy" (e.g., non-zero integers, non-empty strings, populated arrays) evaluate to `true`. Values considered "falsy" (e.g., `0`, `0.0`, `""`, `null`, `[]`, `false`) evaluate to `false`.
3. **Execution:** If the expression is `true`, the statements within the block are executed sequentially.
4. **Re-evaluation:** Once the block completes, execution returns to step 1. This cycle continues until the expression evaluates to `false`.

## State Mutation

To prevent an infinite loop, the code block within the `while` loop must eventually mutate the state of the variables evaluated in the `expression`.

```php theme={"dark"}
$iteration = 0;

while ($iteration < 3) {
    echo $iteration;
    $iteration++; // Mutates state to eventually yield a false condition
}
// Output: 012
```

## Loop Control Statements

PHP provides specific keywords to alter the standard execution flow of a `while` loop from within its block:

* **`break`**: Immediately terminates the execution of the `while` loop. Program control resumes at the next statement following the loop block. You can pass an optional integer argument (e.g., `break 2;`) to break out of multiple nested loops.
* **`continue`**: Halts the current iteration, skipping any remaining statements in the block, and immediately jumps back to the expression evaluation to begin the next iteration. Like `break`, it accepts an optional integer argument to skip iterations of nested loops.

```php theme={"dark"}
$i = 0;

while ($i < 5) {
    $i++;
    
    if ($i === 2) {
        continue; // Skips the echo for $i = 2
    }
    
    if ($i === 4) {
        break; // Terminates the loop entirely
    }
    
    echo $i;
}
// Output: 13
```

<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>
