Bird
0
0

Which of the following is the correct syntax to use the null coalescing operator in PHP?

easy📝 Syntax Q12 of 15
PHP - Operators
Which of the following is the correct syntax to use the null coalescing operator in PHP?
A$value = $a ??= $b;
B$value = $a ?: $b;
C$value = $a ? $b;
D$value = $a ?? $b;
Step-by-Step Solution
Solution:
  1. Step 1: Identify the null coalescing operator syntax

    The operator is written as ?? between two expressions.
  2. Step 2: Check each option

    $value = $a ?? $b; uses $a ?? $b which is correct syntax. $value = $a ?: $b; uses the ternary shorthand, not null coalescing. $value = $a ? $b; is incomplete syntax. $value = $a ??= $b; uses ??= which is a different operator (null coalescing assignment).
  3. Final Answer:

    $value = $a ?? $b; -> Option D
  4. Quick Check:

    Correct null coalescing syntax uses ?? between values [OK]
Quick Trick: Use ?? between two values for null coalescing [OK]
Common Mistakes:
  • Confusing ?? with ?: operator
  • Using incomplete or invalid syntax
  • Mixing null coalescing with assignment operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes