Skip to content
Tutorials Center
My learning

Topics

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
<?php if ($user): ?>
  <p>Hello, <?= htmlspecialchars($user) ?></p>
<?php else: ?>
  <p>Please sign in.</p>
<?php endif; ?>

Try it

PHP Mixing PHP and HTML

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.

PHP All the alternative forms

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

PHP
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 }, and if (...): with endif;. Both are fine in the same file for different statements.
  • else if versus elseif. With a colon you must write elseif. The two-word else if is a parse error in the alternative syntax.
  • switch quirk. Nothing, not even whitespace-only HTML, may sit between switch (...): and the first case; 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.
  • break and continue work as in the brace form; see break and continue.
  • Template engines. Blade, Twig and similar tools compile their own syntax to something like this, so it is useful to recognize.