What does the null coalescing operator (??) do in PHP?
It returns the value of the left operand if it exists and is not null; otherwise, it returns the right operand.
Click to reveal answer
intermediate
How is the null coalescing operator different from the ternary operator?
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 that condition.
Click to reveal answer
beginner
Example: What will this code output?
$name = null; echo $name ?? 'Guest';
It will output 'Guest' because $name is null, so the operator returns the right side value.
Click to reveal answer
intermediate
Can the null coalescing operator be chained? Give an example.
Yes, it can be chained to check multiple variables. Example:
When was the null coalescing operator introduced in PHP?
It was introduced in PHP 7.0 to simplify checking for null or unset variables.
Click to reveal answer
What will this PHP code output?
$a = null; $b = 'Hello'; echo $a ?? $b;
Anull
BHello
CError
DEmpty string
✗ Incorrect
Since $a is null, the operator returns $b which is 'Hello'.
Which PHP version introduced the null coalescing operator (??)?
APHP 5.6
BPHP 7.4
CPHP 7.0
DPHP 8.0
✗ Incorrect
The null coalescing operator was introduced in PHP 7.0.
What does the null coalescing operator check for?
AIf a variable is true
BIf a variable is empty string
CIf a variable is false
DIf a variable is set and not null
✗ Incorrect
It checks if the variable exists and is not null.
Which of these is a valid use of the null coalescing operator?
A$value = $x ?? 'default';
B$value = $x ? 'default';
C$value = $x ?: 'default';
D$value = $x || 'default';
✗ Incorrect
The syntax $x ?? 'default' is correct for null coalescing.
Can the null coalescing operator be used to check multiple variables in one expression?
AYes, by chaining multiple ?? operators
BNo, only one variable at a time
COnly with arrays
DOnly inside functions
✗ Incorrect
You can chain ?? operators to check several variables in order.
Explain how the null coalescing operator works in PHP and give a simple example.
Think about checking if a variable exists or has a value, then providing a fallback.
You got /3 concepts.
Describe a situation where chaining the null coalescing operator is useful.
Imagine getting user input from different sources with fallback defaults.
You got /3 concepts.
Practice
(1/5)
1. What does the null coalescing operator ?? do in PHP?
easy
A. Returns the first operand if it is not null, otherwise returns the second operand.
B. Returns true if both operands are null.
C. Checks if a variable is set and throws an error if not.
D. Assigns null to a variable if it is not already set.
Solution
Step 1: Understand the operator's purpose
The null coalescing operator ?? checks if the left value is null.
Step 2: Determine the returned value
If the left value is not null, it returns that; otherwise, it returns the right value.
Final Answer:
Returns the first operand if it is not null, otherwise returns the second operand. -> Option A
Quick Check:
Null coalescing operator returns first non-null value [OK]
Hint: Remember: returns first non-null value [OK]
Common Mistakes:
Thinking it returns true/false
Confusing it with the ternary operator
Assuming it throws errors if variable is unset
2. Which of the following is the correct syntax to use the null coalescing operator in PHP?
easy
A. $value = $a ??= $b;
B. $value = $a ?: $b;
C. $value = $a ? $b;
D. $value = $a ?? $b;
Solution
Step 1: Identify the null coalescing operator syntax
The operator is written as ?? between two expressions.
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).
Final Answer:
$value = $a ?? $b; -> Option D
Quick Check:
Correct null coalescing syntax uses ?? between values [OK]
Hint: Use ?? between two values for null coalescing [OK]
Common Mistakes:
Confusing ?? with ?: operator
Using incomplete or invalid syntax
Mixing null coalescing with assignment operator
3. What will be the output of the following PHP code?
$a = null; $b = 5; $c = $a ?? $b; echo $c;
medium
A. null
B. 0
C. 5
D. Error
Solution
Step 1: Evaluate the variables
$a is null, $b is 5.
Step 2: Apply null coalescing operator
$c = $a ?? $b means if $a is not null use $a, else use $b. Since $a is null, $c becomes 5.
Final Answer:
5 -> Option C
Quick Check:
Null coalescing returns 5 when left is null [OK]
Hint: If left is null, output right value [OK]
Common Mistakes:
Assuming output is null
Confusing with ternary operator behavior
Expecting error due to null variable
4. Identify the error in the following PHP code using the null coalescing operator:
$value = ?? 10;
medium
A. Missing left operand before ?? operator.
B. Missing semicolon at the end.
C. Using wrong operator, should be ?: instead of ??.
D. No error, code is correct.
Solution
Step 1: Analyze the operator usage
The null coalescing operator requires a left and right operand, like $a ?? $b.
Step 2: Identify the error in the code
The code has ?? 10 without a left operand before ??, which is invalid syntax.
Final Answer:
Missing left operand before ?? operator. -> Option A
Quick Check:
Null coalescing needs both operands [OK]
Hint: Both sides needed for ?? operator [OK]
Common Mistakes:
Leaving out left operand
Confusing ?? with ternary operator
Ignoring syntax errors
5. Given the array $data = ['name' => null, 'age' => 25];, which code snippet correctly assigns $result to the value of 'name' if it exists and is not null, otherwise to 'Unknown'?
hard
A. $result = $data['name'] ?? 'Unknown';
B. $result = isset($data['name']) ? $data['name'] : 'Unknown';
C. $result = $data['name'] ?: 'Unknown';
D. $result = $data['name'] ??= 'Unknown';
Solution
Step 1: Understand the difference between ?? and isset()
The null coalescing operator returns the right value if the left is null or not set. However, if the key exists with a null value, $data['name'] ?? 'Unknown' returns 'Unknown'.
Step 2: Check each option's behavior
$result = $data['name'] ?? 'Unknown'; returns 'Unknown' because 'name' is null. $result = isset($data['name']) ? $data['name'] : 'Unknown'; uses isset() which returns false if null, so it returns 'Unknown'. $result = $data['name'] ?: 'Unknown'; uses ?: which treats null as false, so returns 'Unknown'. $result = $data['name'] ??= 'Unknown'; uses ??= which assigns 'Unknown' to $data['name'] if null or unset, then returns it.
Step 3: Choose the best option for the requirement
The question asks to assign $result to 'name' if it exists and is not null, else 'Unknown'. Since 'name' exists but is null, only $result = isset($data['name']) ? $data['name'] : 'Unknown'; correctly checks existence and returns 'Unknown'. $result = $data['name'] ?? 'Unknown'; and C treat null as missing, $result = $data['name'] ??= 'Unknown'; modifies the array which is not asked.
Final Answer:
$result = isset($data['name']) ? $data['name'] : 'Unknown'; -> Option B
Quick Check:
Use isset() to check existence and null separately [OK]
Hint: Use isset() to check key exists and not null [OK]
Common Mistakes:
Assuming ?? checks key existence only
Using ??= which changes original array
Confusing ?: with ?? operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.