Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.

This section summarizes control flow in Rust and PHP.

Key points

  • Conditionals: Rust if/else require bool; PHP coerces to boolean. [See 2.31]
  • Loops: Rust has loop, while, for (iterator-based); PHP has while, do-while, for, foreach. [See 2.32–2.37]
  • Pattern matching: Rust match is exhaustive and supports destructuring; PHP match (8.0+) compares by identity. [See 2.38–2.45]
  • Break/continue: Both support break and continue; Rust break can return a value.

Comparison

  • Match vs switch: Rust match is expression-based and exhaustive; PHP match is expression-based with default.
  • For loops: Rust for iterates; PHP for is C-style; PHP foreach for arrays.
  • Ranges: Rust uses a..b and a..=b; PHP uses range() or C-style for.
  • Guards: Rust match guards (if after pattern); PHP uses conditionals.

References

What's next

Next: if and else statements.

From PHP to Rust — Rust Syntax and Functions