0
0
PHPprogramming~10 mins

Ternary operator in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A'No'
B'Yes'
Ctrue
Dfalse
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'.
2fill in blank
medium

Complete 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'
A'minor'
B$age
C18
D'adult'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'adult' and 'minor' values.
Using the number 18 instead of the string 'adult'.
3fill in blank
hard

Fix 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'
A'Success'
B'Error'
C200
DSuccess
Attempts:
3 left
💡 Hint
Common Mistakes
Using Success without quotes causing undefined constant error.
Using 200 instead of the string 'Success'.
4fill in blank
hard

Fill 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'
A%
B'even'
C'odd'
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of % for modulus.
Assigning 'odd' in the true case.
5fill in blank
hard

Fill 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'
Astrtoupper
B$word
Cstrtolower
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same function for both true and false cases.
Not passing $word as argument.