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
The endswitch statement is used to terminate a switch statement in PHP when using the alternative colon syntax. It is a required statement in this syntax and must be used to signal the end of the switch block.
Example
Here's an example that demonstrates the use of endswitch in PHP:
<?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;In the example above, we have a switch block that tests the value of the variable $dayOfWeek. Depending on the value of $dayOfWeek, a different case block will be executed.
At the end of each case block, a break statement is used to exit the switch block and prevent further case blocks from being executed. The default case is executed if none of the previous case blocks match the value of $dayOfWeek.
Finally, the endswitch statement is used to signal the end of the switch block.
It's important to note that endswitch is mandatory when using the alternative colon syntax for switch statements. If you use the standard syntax with curly braces {}, endswitch is not used. The alternative syntax is often preferred for readability, especially when mixing PHP with HTML templates.
Practice
What does the 'endswitch' statement signify in PHP?