How come a case option in a switch statement that does not contain a break automatically forwards to a next case without check?
try {
switch($param) {
In PHP 8 we have match
, similar with switch
expression but is significantly shorter:
An example:
$message = match ($statusCode) {
200, 300 => null,
400 => 'not found',
500 => 'server error',
default => 'unknown status code',
};
Here's its switch
equivalent:
switch ($statusCode) {
case 200:
case 300:
$message = null;
break;
case 400:
$message = 'not found';
break;
case 500:
$message = 'server error';
break;
default:
$message = 'unknown status code';
break;
}
reference : https://stitcher.io/blog/php-8-match-or-switch