PHP Reference ยท Language Constructs and Statements
PHP alternative control structure syntax
Reference for PHP's alternative control syntax: if/elseif/else/endif, foreach/endforeach, for, while and switch with colons, mainly used in templates.
PHP has a second way to write control structures. Instead of curly braces, you put a colon after the condition and close the block with a keyword: endif, endforeach, endfor, endwhile or endswitch. The meaning is identical to the brace form.
The point is readability in templates, where PHP is mixed with HTML. A closing endif; is easier to spot between lines of markup than a lone }. In files that contain only PHP code, braces remain the standard.
<?php if ($user): ?> <p>Hello, <?= htmlspecialchars($user) ?></p> <?php else: ?> <p>Please sign in.</p> <?php endif; ?>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
if (condition): elseif (condition): else: endif; while (condition): endwhile; for (init; test; step): endfor; foreach ($array as $v): endforeach; switch ($x): case 1: endswitch; declare(ticks=1): enddeclare;
| Structure | Closing keyword |
|---|---|
if, elseif, else |
endif; |
while |
endwhile; |
for |
endfor; |
foreach |
endforeach; |
switch |
endswitch; |
declare |
enddeclare; |
Return value: none; these are statements.
Errors: a missing endif;, or mixing a colon opening with a closing brace, is a parse error.
Usage notes
- Use it in templates only. Pure PHP files read better with braces, and PSR-12 style expects them there.
- Don't mix styles in one block.
if (...) {must end with}, andif (...):withendif;. Both are fine in the same file for different statements. else ifversuselseif. With a colon you must writeelseif. The two-wordelse ifis a parse error in the alternative syntax.switchquirk. Nothing, not even whitespace-only HTML, may sit betweenswitch (...):and the firstcase; output there is a parse error. Write<?php switch ($x): ?>then<?php case 1: ?>with no text in between.- Short echo tag.
<?= expr ?>is always available and is a shortcut for<?php echo expr; ?>. See PHP tags and embedding. - A closing tag eats one newline.
?>directly followed by a newline swallows that newline, which keeps template output tidy. - Escape output. Always pass strings through
htmlspecialchars()when printing user data in HTML; see XSS prevention. breakandcontinuework as in the brace form; seebreakandcontinue.- Template engines. Blade, Twig and similar tools compile their own syntax to something like this, so it is useful to recognize.
Related
if, elseif, else,foreach,switch,while,for, PHP tags and embedding- Lessons: PHP and HTML, If / else