The default Construct
The default construct is used inside the switch block and
is executed if none of the case options match
the passed value. It is similar to the else condition in the
if-else construct.
Syntax
switch ($var) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}
Example
Let's check the variable's value and output a default message:
<?php
$day = 'Sunday';
switch ($day) {
case 'Monday':
echo 'Start of work week';
break;
case 'Friday':
echo 'End of work week';
break;
default:
echo 'Weekend or other day';
}
?>
Code execution result:
'Weekend or other day'
Example
Using default with numeric values:
<?php
$num = 5;
switch ($num) {
case 1:
echo 'One';
break;
case 2:
echo 'Two';
break;
default:
echo 'Number not in range';
}
?>
Code execution result:
'Number not in range'