switch
switch (value) compares the value with each case, in order. It runs the code from the first
case that is equal. break ends the switch.
switch
switch (value) { … } compares one value with a list of labels and runs the body of the first label that is equal to it. Each label is written as case something:, and default: is what runs when no label matched. Nothing happens when no label matched and there is no default.
A body runs on into the next body until a break stops it. That is called falling through, and it lets several labels share one body: write them one under the other, with nothing in between. A body that ends with return also leaves the switch.
The labels are tried in the order you wrote them, and default is tried last wherever you put it. switch (true) compares against true, so every label is a condition, and the first condition that is true is the one that runs.
The examples below show a choice between three answers, then labels that share a body, then switch (true) sorting a number into bands, then a default between two labels.
Choosing one answer out of several
<?nvs// Shows a `switch` that picks one answer out of several. The subject is// compared with each `case` in order, and `default` runs when none matched.
array<string> $statuses = ['new', 'sent', 'lost'];
foreach ($statuses as string $status) { string $text = ''; switch ($status) { case 'new': $text = 'waiting to be packed'; break; case 'sent': $text = 'on its way'; break; default: $text = 'we do not know'; } echo $status, ': ', $text, "\n";}new: waiting to be packedsent: on its waylost: we do not knowTwo labels that share one body
<?nvs// Shows labels that share a body, and a body without `break`. A body without// `break` runs on into the next body, which is called falling through.
array<string> $keys = ['a', 's', 'q'];
foreach ($keys as string $key) { echo $key, ': '; switch ($key) { case 'a': case 's': echo 'saving'; break; case 'q': echo 'saving, '; // There is no `break` here, so the next body runs as well. default: echo 'leaving'; } echo "\n";}a: savings: savingq: saving, leavingSorting a number into bands
<?nvs// Shows `switch (true)`, which tests one condition after another and runs the// first one that is true. A program does this to sort a number into bands.
class Shipping{ public static function band(uint $grams): string { switch (true) { case $grams <= 500: return 'letter'; case $grams <= 2000: return 'small parcel'; case $grams <= 20000: return 'parcel'; default: return 'freight'; } }}
array<uint> $weights = [120, 1500, 9000, 45000];
foreach ($weights as uint $grams) { // This prints letter, small parcel, parcel and freight, one per line. echo $grams, 'g: ', Shipping::band($grams), "\n";}120g: letter1500g: small parcel9000g: parcel45000g: freightA default in the middle
<?nvs// Shows a `default` written between two labels. It is still tried last, and// it falls through into the body after it.
array<int> $codes = [1, 2, 9];
foreach ($codes as int $code) { echo $code, ': '; switch ($code) { case 1: echo 'one, '; default: echo 'other, '; case 2: echo 'two'; } echo "\n";}
// This prints "1: one, other, two", then "2: two", then "9: other, two".1: one, other, two2: two9: other, twoDetails
switch compares the value with each label using ==. A label whose type can never be equal to the value is the error E0466. An int value with the label "5" is one such case.
A body falls through into the body written after it, even when that is default. default is still tried last, after every case. When it matches, the bodies after it run too, until a break.
Use this when each branch runs several statements
Section titled “Use this when each branch runs several statements”For a branch that only gives a value, match is shorter, and it
never runs the next branch when you forget a break.
Do and don’t
Section titled “Do and don’t”- Do end every
casebody withbreak, unless the nextcasemust run too. - Do write a comment above a
casebody that has nobreak. It tells the reader that the nextcasemust run too. - Don’t use
switchto pick a value. Usematch. - Don’t write
continueinside aswitchto end it.continuestarts the next pass of the loop around theswitch.
