abi*_*rth 3 php enums switch-statement
echo $state; //debug
switch($state){
case "Sleeping":
$label = "Is Sleeping";
case "Running":
$label = "Is Running";
default:
$label = "Could not retrieve state";
}
Run Code Online (Sandbox Code Playgroud)
$state从SQL数据库(Enum类型)填充,回显消息打印“Sleeping”,但$label填充默认值
小智 8
在您的情况下,选择match(PHP 8)表达式明显更短,并且不需要语句break。
$state = "Running";
$label = match ($state) {
"Sleeping"=> "Is Sleeping",
"Running" => "Is Running",
default => "Could not retrieve state"
};
echo $label; // "Is Running"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3620 次 |
| 最近记录: |