Bird
0
0

Given this array:

hard📝 Application Q8 of 15
PHP - Conditional Statements
Given this array:
$data = ['name' => null, 'age' => 25];
// Use null coalescing to get 'name' or 'Unknown'
$name = $data['name'] ?? 'Unknown';
echo $name;

What will be the output?
AError
Bnull
Cname
DUnknown
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of $data['name']

    $data['name'] is explicitly set to null.
  2. Step 2: Apply null coalescing operator

    Since $data['name'] is null, $data['name'] ?? 'Unknown' returns 'Unknown'.
  3. Final Answer:

    Unknown -> Option D
  4. Quick Check:

    Null coalescing returns default if value is null [OK]
Quick Trick: Null coalescing returns default for null array values [OK]
Common Mistakes:
  • Expecting null output
  • Confusing with isset()
  • Thinking it causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes