What does the null coalescing operator (??) do in PHP?
It returns the left-hand operand if it exists and is not null; otherwise, it returns the right-hand operand.
Click to reveal answer
intermediate
How can you use null coalescing in an if condition to provide a default value?
You can write: if (($value = $array['key'] ?? 'default') === 'default') { ... } to check if the key exists or use 'default' if not.
Click to reveal answer
intermediate
What is the difference between the null coalescing operator (??) and the ternary operator (?:) in PHP?
The null coalescing operator only checks if a value is set and not null, while the ternary operator evaluates a condition and returns one of two values based on true or false.
Click to reveal answer
intermediate
Can the null coalescing operator be chained in PHP? Give an example.
Yes, you can chain it like: $value = $a ?? $b ?? $c ?? 'default'; which returns the first non-null value among $a, $b, $c, or 'default'.
Click to reveal answer
beginner
Why is null coalescing useful in conditions when working with arrays?
It helps avoid errors when accessing undefined array keys by providing a fallback value, making code safer and cleaner.
Click to reveal answer
What will $value = $array['key'] ?? 'default'; assign if $array['key'] is not set?
A0
Bnull
CAn error
D'default'
✗ Incorrect
The null coalescing operator returns 'default' if $array['key'] is not set or is null.
Which operator checks if a variable is set and not null in PHP?
A?? (null coalescing)
B?: (ternary)
C&& (and)
D|| (or)
✗ Incorrect
The null coalescing operator (??) checks if a variable is set and not null.
What happens if you chain null coalescing operators like $a ?? $b ?? $c?
AThrows an error
BReturns the first non-null value among $a, $b, or $c
CReturns null always
DReturns the last value always
✗ Incorrect
Chaining returns the first operand that is set and not null.
Which of these is a correct use of null coalescing in an if condition?
Aif (($val = $data['x'] ?? 10) > 5) { ... }
Bif ($data['x'] ?? 10 > 5) { ... }
Cif ($data['x'] ?: 10 > 5) { ... }
Dif ($data['x'] && 10 > 5) { ... }
✗ Incorrect
Assigning with null coalescing inside the condition is valid and clear.
Why might null coalescing be preferred over isset() in conditions?
AIt runs faster always
BIt throws errors if variable is missing
CIt is shorter and more readable
DIt only works with objects
✗ Incorrect
Null coalescing is concise and improves readability compared to isset() checks.
Explain how the null coalescing operator (??) works in PHP and give an example of its use in a condition.
Think about how to safely get a value or a default.
You got /4 concepts.
Describe the benefits of using null coalescing in conditions when accessing array elements.
Consider what happens if the key does not exist.
You got /3 concepts.
Practice
(1/5)
1. What does the null coalescing operator ?? do in PHP conditions?
easy
A. Always returns the second operand regardless of the first.
B. Returns the first operand if it exists and is not null; otherwise returns the second operand.
C. Checks if both operands are null and returns true if so.
D. Throws an error if the first operand is null.
Solution
Step 1: Understand the operator's purpose
The null coalescing operator ?? checks if the left value exists and is not null.
Step 2: Determine the return value
If the left value is null or missing, it returns the right value as a fallback.
Final Answer:
Returns the first operand if it exists and is not null; otherwise returns the second operand. -> Option B
Quick Check:
Null coalescing = first non-null value [OK]
Hint: 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
2. Which of the following is the correct syntax to use null coalescing in a PHP if condition?
easy
A. if ($value || 'default') { ... }
B. if ($value ?: 'default') { ... }
C. if ($value ?? 'default') { ... }
D. if ($value ??= 'default') { ... }
Solution
Step 1: Identify null coalescing syntax
The null coalescing operator is ?? and used like $value ?? 'default'.
Step 2: Check each option
if ($value ?? 'default') { ... } uses ?? correctly inside the if condition. if ($value ?: 'default') { ... } uses the ternary shortcut, not null coalescing. if ($value || 'default') { ... } uses logical OR, which is different. if ($value ??= 'default') { ... } uses ??= which is an assignment operator, not a condition.
The expression $name ?? 'Guest' returns 'Guest' because $name is null.
Final Answer:
Guest -> Option A
Quick Check:
null ?? 'Guest' = 'Guest' [OK]
Hint: Null on left means right value is chosen [OK]
Common Mistakes:
Thinking null prints as 'null' string
Confusing null coalescing with OR operator
Expecting error when variable is null
4. Find the error in this PHP code snippet using null coalescing:
if ($user ?? 'guest') { echo 'Welcome!'; }
medium
A. The condition always evaluates to true, so it may not work as intended.
B. The variable $user must be checked with isset() before using ??.
C. The null coalescing operator cannot be used inside if conditions.
D. The operator ?? should be replaced with logical OR ||.
Solution
Step 1: Understand the condition logic
The expression $user ?? 'guest' returns $user if set and not null, else 'guest'.
Step 2: Analyze boolean evaluation
Since 'guest' is a non-empty string, the condition is always true, even if $user is null or false.
Final Answer:
The condition always evaluates to true, so it may not work as intended. -> Option A
Quick Check:
Non-null fallback makes condition always true [OK]
Hint: Check if fallback value makes condition always true [OK]
Common Mistakes:
Assuming null coalescing checks boolean truth
Thinking isset() is required with ?? operator
Replacing ?? with || without reason
5. You want to display a username if it exists and is not null, otherwise show 'Anonymous'. Which code correctly uses null coalescing in a condition to echo the username or fallback?
hard
A. echo $username || 'Anonymous';
B. echo isset($username) ? $username : 'Anonymous';
C. echo $username ?: 'Anonymous';
D. echo $username ?? 'Anonymous';
Solution
Step 1: Understand the requirement
We want to print $username if it exists and is not null; otherwise print 'Anonymous'.
Step 2: Compare options
echo isset($username) ? $username : 'Anonymous'; uses ternary with isset(), which works but is longer. echo $username ?? 'Anonymous'; uses null coalescing ?? which directly checks for null and provides fallback. echo $username ?: 'Anonymous'; uses the ternary shortcut ?: which treats empty strings or 0 as false, not just null. echo $username || 'Anonymous'; uses logical OR which is incorrect syntax for echo.
Final Answer:
echo $username ?? 'Anonymous'; -> Option D
Quick Check:
Null coalescing for fallback = echo $username ?? 'Anonymous'; [OK]
Hint: Use ?? to fallback only on null, not falsy values [OK]
Common Mistakes:
Using ?: which treats empty strings as false
Using logical OR which is invalid here
Overcomplicating with isset() ternary
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.