endswitch
The endswitch statement is used to terminate a switch statement in PHP. It is a required statement in a switch block and must be used to signal the end of the
Introduction
endswitch closes a switch statement written in PHP's alternative syntax — the form that replaces the opening brace { with a colon : and the closing brace } with the keyword endswitch;. It is not a separate language feature so much as the closing delimiter of one particular way of writing switch.
PHP gives you two equivalent ways to write block statements like switch, if, for, foreach, and while:
- Curly-brace syntax —
switch (...) { ... } - Alternative (colon) syntax —
switch (...): ... endswitch;
This page explains when endswitch is required, why the alternative syntax exists, and the one gotcha that trips people up.
Syntax: braces vs. endswitch
The two snippets below do exactly the same thing. The only difference is how the block is opened and closed.
<?php
// Curly-brace syntax — no endswitch
switch ($n) {
case 1:
echo "one";
break;
default:
echo "other";
}<?php
// Alternative syntax — colon opens, endswitch closes
switch ($n):
case 1:
echo "one";
break;
default:
echo "other";
endswitch;endswitch is mandatory with the colon form and invalid with the brace form — you pick one style per switch and stay consistent. Mixing them (a : opener with a } closer, or vice versa) is a parse error.
Example
Here's a complete, runnable example using the alternative syntax:
<?php
$dayOfWeek = 2;
switch ($dayOfWeek):
case 1:
echo "Today is Monday";
break;
case 2:
echo "Today is Tuesday";
break;
case 3:
echo "Today is Wednesday";
break;
case 4:
echo "Today is Thursday";
break;
case 5:
echo "Today is Friday";
break;
default:
echo "It is the weekend!";
endswitch;Because $dayOfWeek is 2, the case 2: branch runs and prints Today is Tuesday. The break at the end of each branch exits the switch so the following cases don't run, and default: handles any value that matches no case. The endswitch; line closes the structure; execution continues with whatever comes after it.
Why use the alternative syntax?
The colon form exists mainly for templates that interleave PHP and HTML. When the body of a control structure spans many lines of markup, a stray } is easy to misplace and hard to match by eye. The named keyword endswitch makes the closing point obvious — you can see at a glance that it pairs with switch, the same way </div> pairs with <div>.
This pattern is common in plain-PHP templates (and in frameworks that compile to them):
<?php switch ($status): ?>
<?php case 'active': ?>
<p>Your account is active.</p>
<?php break; ?>
<?php case 'pending': ?>
<p>Your account is awaiting approval.</p>
<?php break; ?>
<?php default: ?>
<p>Account status unknown.</p>
<?php endswitch; ?>Here the cases output HTML directly instead of using echo. The alternative syntax keeps each <?php ?> tag short and the closing endswitch self-documenting. With braces, the closer would be a lone <?php } ?> — far less readable inside a wall of markup.
Common gotcha: no output before the first case
In the alternative switch syntax you must not emit any HTML or whitespace between the switch(...): line and the first case. In a template, text placed there is sent to the browser unconditionally and PHP raises a warning, so keep the opening tag tight against the first <?php case ...: ?>.
Related references
switchand PHPswitch— the statementendswitchclosesbreak— exits acaseso execution doesn't fall throughendif,endfor,endforeach— the same alternative-syntax closers for other control structures