Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to assign $result to 'Yes' if $a is true, otherwise 'No'.
PHP
$result = $a ? [1] : 'No';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting 'No' as the first value, which is for the false case.
Using true or false instead of the string 'Yes'.
✗ Incorrect
The ternary operator syntax is condition ? value_if_true : value_if_false. Here, if $a is true, $result should be 'Yes'.
2fill in blank
mediumComplete the code to assign $status to 'adult' if $age is 18 or more, otherwise 'minor'.
PHP
$status = ($age >= 18) ? [1] : 'minor';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'adult' and 'minor' values.
Using the number 18 instead of the string 'adult'.
✗ Incorrect
If $age is 18 or more, $status should be 'adult'. The ternary operator returns 'adult' when the condition is true.
3fill in blank
hardFix the error in the ternary operator to assign $message to 'Success' if $code equals 200, else 'Error'.
PHP
$message = ($code == 200) ? [1] : 'Error';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Success without quotes causing undefined constant error.
Using 200 instead of the string 'Success'.
✗ Incorrect
In PHP, strings must be in quotes. 'Success' is the correct string value to assign.
4fill in blank
hardFill both blanks to assign $output to 'even' if $num is divisible by 2, else 'odd'.
PHP
$output = ($num [1] 2 == 0) ? [2] : 'odd';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of % for modulus.
Assigning 'odd' in the true case.
✗ Incorrect
The modulus operator % checks divisibility. If remainder is 0, $num is even, so assign 'even'.
5fill in blank
hardFill all three blanks to create a ternary that assigns $result to uppercase $word if $flag is true, else lowercase $word.
PHP
$result = $flag ? [1]([2]) : [3]($word);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same function for both true and false cases.
Not passing $word as argument.
✗ Incorrect
If $flag is true, convert $word to uppercase using strtoupper($word), else lowercase with strtolower($word).