The PHP ?? operator seems to ignore == and === in the same expression
$status ?? 'active' === 'active' returns the value of $status, not true or falseShort answer
?? ranks below ==, ===, && and ||, so $a ?? $b === $c is read as $a ?? ($b === $c). When $a is set, the comparison never matters and you get $a back. Put brackets around the ?? part. Two more traps: ?? falls back only on null, and == converts types before it compares.
Fix it fast
1Find out which cause you have
Add the brackets PHP assumes, and the brackets you meant, and print both. If the two lines differ, precedence is your problem.
<?php $a = 'pending'; var_dump($a ?? 'x' === 'x'); // PHP reads: $a ?? ('x' === 'x') var_dump(($a ?? 'x') === 'x'); // the grouping you meant2Match what you see, then jump to its fix
- 1
The expression returns the variable's own value, or a truthy value, instead of true or false?? binds more loosely than the comparison next to itFix - 2
A default is not applied when the value is an empty string, 0 or false, for example for ?page= in a URL?? falls back only on null, so '', 0 and false are keptFix - 3
A comparison you expect to be false is true when the variable is null or unset== converts types, so null equals false, 0 and an empty stringFix
- 1
What does the error look like?
$status ?? 'active' === 'active' returns the value of $status, not true or falseif ($count ?? 0 > 5) is true for any non-zero $count that is setA warning that $name is undefined, although ?? was used together with a concatenation
Cause 1 of 3
?? binds more loosely than the comparison next to it
1Check
In PHP's precedence table, == and === rank above && and ||, which rank above ??. So the comparison on the right-hand side is evaluated first, as the fallback value. The null coalescing section of the manual says ?? has low precedence and that mixing it with other operators will likely need brackets.
<?php
$count = 3;
var_dump($count ?? 0 > 5); // int(3): the comparison is the fallback
var_dump(($count ?? 0) > 5); // bool(false)2Fix
Put brackets around the part that uses ??, then compare. The same rule applies to concatenation and arithmetic.
<?php
if (($count ?? 0) > 5) { /* ... */ }
$isActive = ($status ?? 'active') === 'active';
print 'Mr. ' . ($name ?? 'Anonymous');3Verify
Run the two-line test from the first step again; both lines now give the same result.
<?php
$a = 'pending';
var_dump(($a ?? 'x') === 'x'); // bool(false)Confirmed by php.net
Cause 2 of 3
?? falls back only on null, so '', 0 and false are kept
1Check
?? gives the right-hand side only when the left-hand side is null, and it works like isset(). Any other value, including an empty string, passes through unchanged.
<?php
var_dump('' ?? 'default'); // string(0) ""
var_dump(0 ?? 'default'); // int(0)
var_dump(false ?? 'default'); // bool(false)2Fix
If empty values should fall back too, apply the short ternary ?: on top. It returns its left side when that side is true. Keep ?? inside it, so a missing key raises no warning.
<?php
$page = ($_GET['page'] ?? null) ?: 1;3Verify
Request the page with ?page= and with no parameter; both give 1.
<?php
var_dump(($_GET['page'] ?? null) ?: 1);Confirmed by php.net
Cause 3 of 3
== converts types, so null equals false, 0 and an empty string
1Check
With == PHP juggles types. A null compared with anything is turned into a bool, and a null compared with a string becomes an empty string. === also compares the type.
<?php
var_dump(null == false, null == 0, null == ''); // true, true, true
var_dump(null === false, null === 0, null === ''); // false, false, false2Fix
Compare with === after the ?? fallback when the type matters. Pick a fallback that cannot collide with a real value, such as null itself.
<?php
$level = $_GET['level'] ?? null;
if ($level === null) {
// parameter missing
} elseif ($level === '0') {
// parameter present and equal to the string 0
}3Verify
Run the three-line check; each comparison now reports what you intended.
<?php
var_dump(($_GET['level'] ?? null) === null);Confirmed by php.net
How do I stop it coming back?
- Always put brackets around a ?? expression that sits next to a comparison, a concatenation or arithmetic.
- Prefer === unless you want type juggling; read the comparison table in the manual before using == with null.
- Use ?? for missing values and ?: for empty ones, and do not expect one to do the other's job.
- Make grouping explicit even where PHP does not need it; the manual says this often makes code easier to read.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Warning: Undefined variable and Undefined array key in PHP
Warning: Undefined array key "email" in /var/www/app/form.php on line 8
PHP read a variable or array key that was never set.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix