Complete the code to return the correct day name using match expression.
<?php $dayNumber = 3; $dayName = match($dayNumber) { 1 => 'Monday', 2 => 'Tuesday', [1] => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday', default => 'Invalid day' }; echo $dayName;
The match expression checks the value of $dayNumber. For 3, it should return 'Wednesday'. So the blank must be filled with 3.
Complete the code to return the correct message for a given status code using match expression.
<?php $statusCode = 404; $message = match($statusCode) { 200 => 'OK', 400 => 'Bad Request', [1] => 'Not Found', 500 => 'Server Error', default => 'Unknown Status' }; echo $message;
The status code 404 corresponds to 'Not Found'. So the blank must be 404.
Fix the error in the match expression to correctly handle fruit colors.
<?php $fruit = 'apple'; $color = match($fruit) { 'banana' => 'yellow', 'apple' => [1], 'grape' => 'purple', default => 'unknown' }; echo $color;
In PHP, string values in match arms must be quoted. So 'red' is correct. Using unquoted red causes an error.
Fill both blanks to create a match expression that returns the season for a given month number.
<?php $month = 11; $season = match(true) { $month >= 3 && $month <= 5 => [1], $month >= 6 && $month <= 8 => [2], $month >= 9 && $month <= 11 => 'Autumn', default => 'Winter' }; echo $season;
The match expression uses conditions to check the month. Months 3-5 are Spring, 6-8 are Summer. So blanks must be 'Spring' and 'Summer'.
Fill all three blanks to create a match expression that returns the type of a variable.
<?php $var = 3.14; $type = match(gettype($var)) { [1] => 'integer', [2] => 'string', [3] => 'float', default => 'other' }; echo $type;
The gettype() function returns 'integer', 'string', or 'double' for floats. So the blanks must be 'integer', 'string', and 'double'.