What if you could replace long, messy checks with a simple symbol that does it all?
Why Null coalescing operator in PHP? - Purpose & Use Cases
Imagine you are checking user input on a website form. You want to use a value if it exists, but if it doesn't, you want to use a default. Doing this by hand means writing many checks for whether each value is set or not.
Manually checking if a value exists before using it is slow and clutters your code. It's easy to forget a check, causing errors or warnings. This makes your code hard to read and maintain.
The null coalescing operator lets you quickly pick the first value that exists without writing long if-else checks. It makes your code cleaner and safer by handling missing values smoothly.
$value = isset($_GET['name']) ? $_GET['name'] : 'Guest';
$value = $_GET['name'] ?? 'Guest';
You can write simpler, clearer code that safely uses default values when data is missing.
When building a website, you often want to greet users by name if they provide it, or say "Guest" if they don't. The null coalescing operator makes this easy and clean.
Manually checking for missing values is slow and error-prone.
The null coalescing operator simplifies choosing between a value or a default.
This leads to cleaner, safer, and easier-to-read code.
Practice
?? do in PHP?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 AQuick Check:
Null coalescing operator returns first non-null value [OK]
- Thinking it returns true/false
- Confusing it with the ternary operator
- Assuming it throws errors if variable is unset
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 ?? $bwhich 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 DQuick Check:
Correct null coalescing syntax uses ?? between values [OK]
- Confusing ?? with ?: operator
- Using incomplete or invalid syntax
- Mixing null coalescing with assignment operator
$a = null;
$b = 5;
$c = $a ?? $b;
echo $c;
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 CQuick Check:
Null coalescing returns 5 when left is null [OK]
- Assuming output is null
- Confusing with ternary operator behavior
- Expecting error due to null variable
$value = ?? 10;
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?? 10without a left operand before??, which is invalid syntax.Final Answer:
Missing left operand before ?? operator. -> Option AQuick Check:
Null coalescing needs both operands [OK]
- Leaving out left operand
- Confusing ?? with ternary operator
- Ignoring syntax errors
$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'?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'; usesisset()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$resultto '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 BQuick Check:
Use isset() to check existence and null separately [OK]
- Assuming ?? checks key existence only
- Using ??= which changes original array
- Confusing ?: with ?? operator
Ready to prove it?
Pass the test and this pattern moves unknown
