Bird
0
0

What does the null coalescing operator ?? do in PHP conditions?

easy📝 Conceptual Q11 of 15
PHP - Conditional Statements
What does the null coalescing operator ?? do in PHP conditions?
AAlways returns the second operand regardless of the first.
BReturns the first operand if it exists and is not null; otherwise returns the second operand.
CChecks if both operands are null and returns true if so.
DThrows an error if the first operand is null.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the operator's purpose

    The null coalescing operator ?? checks if the left value exists and is not null.
  2. Step 2: Determine the return value

    If the left value is null or missing, it returns the right value as a fallback.
  3. Final Answer:

    Returns the first operand if it exists and is not null; otherwise returns the second operand. -> Option B
  4. Quick Check:

    Null coalescing = first non-null value [OK]
Quick Trick: Think: "Use first value if valid, else fallback" [OK]
Common Mistakes:
  • Confusing it with logical OR operator
  • Assuming it throws errors on null
  • Believing it always returns second operand

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes