0
0
PHPprogramming~10 mins

Null coalescing in conditions 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 $value to $input if it exists, otherwise assign 'default'.

PHP
$value = $input [1] 'default';
Drag options to blanks, or click blank then click option'
A&&
B?:
C||
D??
Attempts:
3 left
💡 Hint
Common Mistakes
Using '||' which checks for truthiness but can cause unexpected results.
Using '?:' which is the ternary shorthand but behaves differently.
2fill in blank
medium

Complete the condition to check if $data['name'] exists, otherwise use 'Guest'.

PHP
echo $data['name'] [1] 'Guest';
Drag options to blanks, or click blank then click option'
A?:
B??
C||
D&&
Attempts:
3 left
💡 Hint
Common Mistakes
Using '?:' which requires the left side to be truthy, not just set.
Using '||' which can cause type coercion issues.
3fill in blank
hard

Fix the error in the condition to assign $result to $input if set, else 'none'.

PHP
$result = [1] ?? 'none';
Drag options to blanks, or click blank then click option'
Aisset($input)
B$input ??
C$input
Dempty($input)
Attempts:
3 left
💡 Hint
Common Mistakes
Using B, which places ?? twice causing a syntax error.
Using functions like isset($input), which check existence but do not return the value.
4fill in blank
hard

Complete the code to assign $output to $array['key'] if set, else 'missing'.

PHP
$output = $array['key'][1] 'missing';
Drag options to blanks, or click blank then click option'
A[
B]
C??
D?:
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of brackets for array access.
Using the ternary operator instead of null coalescing.
5fill in blank
hard

Complete the code to create a condition that echoes $user['name'] if set, else 'Anonymous', and assigns it to $displayName.

PHP
$displayName = $user['name'] 'Anonymous';
echo [1];
Drag options to blanks, or click blank then click option'
A[
B??
C$displayName
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to close the bracket for array access.
Echoing the array access expression instead of the variable.