0
0
PHPprogramming~10 mins

Null coalescing operator in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Null coalescing operator
Check if variable isset and not null
Yes
Use variable value
END
No
Use fallback value
END
The null coalescing operator checks if a variable exists and is not null; if so, it uses that value, otherwise it uses a fallback.
Execution Sample
PHP
<?php
$username = $_GET['user'] ?? 'guest';
echo $username;
?>
This code sets $username to the 'user' parameter from the URL if it exists; otherwise, it uses 'guest'.
Execution Table
StepExpressionCheckResultValue assigned to $usernameOutput
1$_GET['user'] ?? 'guest'Is $_GET['user'] set and not null?No'guest'guest
2echo $username;Output the value of $usernameN/AN/Aguest
3End of scriptN/AN/AN/AN/A
💡 Execution stops after echoing the value of $username.
Variable Tracker
VariableStartAfter Step 1Final
$usernameundefined'guest''guest'
Key Moments - 2 Insights
Why does the operator check if the variable is set and not just if it is null?
Because the null coalescing operator (??) first checks if the variable exists (is set) and is not null before using it. If the variable is not set at all, it uses the fallback. This is shown in execution_table step 1 where $_GET['user'] is not set, so 'guest' is used.
What happens if $_GET['user'] is set but its value is null?
The operator treats null as if the variable is not set and uses the fallback value. This means it will assign 'guest' to $username, as the operator checks for both existence and non-null value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value is assigned to $username at step 1?
Anull
B'guest'
Cundefined
D'user'
💡 Hint
Refer to the 'Value assigned to $username' column in step 1 of the execution_table.
At which step does the script output the value of $username?
AStep 1
BStep 3
CStep 2
DNo output step
💡 Hint
Check the 'Output' column in the execution_table for the echo statement.
If $_GET['user'] was set to 'alice', how would the value of $username change at step 1?
A'alice'
B'guest'
Cnull
Dundefined
💡 Hint
The null coalescing operator uses the variable's value if it is set and not null, see concept_flow.
Concept Snapshot
Null coalescing operator (??) syntax:
$var = $possibleNull ?? $fallback;
It returns $possibleNull if set and not null,
otherwise returns $fallback.
Useful for default values when variables may be missing or null.
Full Transcript
The null coalescing operator in PHP checks if a variable exists and is not null. If it is, it uses that variable's value; if not, it uses a fallback value. For example, $username = $_GET['user'] ?? 'guest'; means if the 'user' parameter exists in the URL and is not null, $username gets that value; otherwise, it gets 'guest'. The execution table shows step 1 checking if $_GET['user'] is set and not null. Since it is not set, the fallback 'guest' is assigned. Step 2 outputs $username, which is 'guest'. This operator helps avoid errors from undefined variables and provides a simple way to set default values.