P粉2956161702023-07-31 11:28:23
Writing a switch statement requires quite a bit of code. Why not use another array to associate the message with the state in $hold? like this:
$hold = ['warning', 'warning', 'critical', 'ok', 'this is bad']; $messages = ['warning' => 'it is a warning', 'critical' => 'its critical', 'ok' => 'everyhting is ok']; foreach ($hold as $status) { echo ($messages[$status] ?? 'unknown') . PHP_EOL; }
As you can see, I used a foreach() loop to iterate over $hold.
I used an associative array and the special Null coalescing operator. If you don't like this way you can use your switch statement.
For demonstration, please see: https://3v4l.org/jNkfF