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/elserequirebool; PHP coerces to boolean. [See 2.31] - Loops: Rust has
loop,while,for(iterator-based); PHP haswhile,do-while,for,foreach. [See 2.32–2.37] - Pattern matching: Rust
matchis exhaustive and supports destructuring; PHPmatch(8.0+) compares by identity. [See 2.38–2.45] - Break/continue: Both support
breakandcontinue; Rustbreakcan return a value.
Comparison
- Match vs switch: Rust
matchis expression-based and exhaustive; PHPmatchis expression-based withdefault. - For loops: Rust
foriterates; PHPforis C-style; PHPforeachfor arrays. - Ranges: Rust uses
a..banda..=b; PHP usesrange()or C-stylefor. - Guards: Rust match guards (
ifafter pattern); PHP uses conditionals.
References
What's next
Next: if and else statements.
From PHP to Rust — Rust Syntax and Functions