Back to questions
JuniorTheoryCommon
What does the program print?
<?php
$code = '2';
$word = match ($code) {
1, 2 => 'small',
'2' => 'string two',
default => 'other',
};
echo $word, "\n";string two
- Expecting
small, as withswitch:matchcompares with===, so the string'2'does not equal the integer2and skips that arm. - Expecting several arms to print:
matchpicks exactly one arm, the first whose value is identical, and returns that arm's value.
- What happens if you delete the
defaultarm and set$codeto'5'? - How would you rewrite the choice with
switch, and what would it print?