Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

The PHP ?? operator seems to ignore == and === in the same expression

Error$status ?? 'active' === 'active' returns the value of $status, not true or false

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 meant
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2A 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. 3A 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

What does the error look like?

  • $status ?? 'active' === 'active' returns the value of $status, not true or false
  • if ($count ?? 0 > 5) is true for any non-zero $count that is set
  • A 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

Did this fix it?

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

Did this fix it?

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, false

2Fix

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.