
Wikis & learning
Group several states in a switch
A switch can route two different status labels into the same display action. Put the shared behavior after both labels so that the choice is deliberately visible.
A switch can route two different status labels into the same display action. Put the shared behavior after both labels so that the choice is deliberately visible.
Walkthrough
A break ends the selected branch. Omitting it after printing would continue into the next branch and print a second label. Use a default branch when an unfamiliar status can occur.
PHP example
<?php
$state = 'queued';
switch ($state) {
case 'queued':
case 'sending':
echo 'Processing';
break;
case 'sent':
echo 'Delivered';
break;
default:
echo 'Unknown';
}Expected output
ProcessingChange and check
Try the inputs queued, sending and archived. Add a specific archived branch, keeping the default for states you have not anticipated.
Discussion
Sign in to participate.



