php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #75801 Mistake in documentation example for null coalesce operator
Submitted: 2018-01-11 13:23 UTC Modified: 2018-01-11 13:31 UTC
From: pieter at frenssen dot be Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pieter at frenssen dot be
New email:
PHP Version: OS:

 

 [2018-01-11 13:23 UTC] pieter at frenssen dot be
Description:
------------
---
From manual page: http://www.php.net/language.operators.comparison
---

The example given for the null coalesce operator is not correct. This is the current example:

<?php
// Example usage for: Null Coalesce Operator
$action = $_POST['action'] ?? 'default';

// The above is identical to this if/else statement
if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else {
    $action = 'default';
}

?>

It says that it is equivalent to using `isset()` in an if/else statement, but this is not true. If `$_POST['action']` would be set with the value `NULL` then the first expression would assign the value `default`, but the second would assign the value `NULL`.

In fact it is equivalent to using `!empty()`, so the example should be corrected to the following:

<?php
// Example usage for: Null Coalesce Operator
$action = $_POST['action'] ?? 'default';

// The above is identical to this if/else statement
if (!empty($_POST['action'])) {
    $action = $_POST['action'];
} else {
    $action = 'default';
}

?>

or possibly to the following, if we want to keep using `isset()` which is also mentioned elsewhere in the documentation:

<?php
// Example usage for: Null Coalesce Operator
$action = $_POST['action'] ?? 'default';

// The above is identical to this if/else statement
if (isset($_POST['action']) && !is_null($_POST['action'])) {
    $action = $_POST['action'];
} else {
    $action = 'default';
}

?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-11 13:27 UTC] pieter at frenssen dot be
Actually the first suggestion is not correct, `!empty()` also returns TRUE if the value would be set to 0 or FALSE.

So the second suggestion is the only correct one: `isset($_POST['action']) && !is_null($_POST['action'])`.
 [2018-01-11 13:31 UTC] daverandom@php.net
-Status: Open +Status: Not a bug
 [2018-01-11 13:31 UTC] daverandom@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

isset($var) returns FALSE for variables that have the value NULL.

https://3v4l.org/54jgH
 [2018-01-11 13:34 UTC] daverandom@php.net
See also this example using the exact code from the documentation:

https://3v4l.org/PR86b
 [2018-01-11 13:40 UTC] daverandom@php.net
Better example https://3v4l.org/m9KoC
 [2018-01-11 21:07 UTC] pieter at frenssen dot be
You're right, my assumption was wrong. Thanks for providing the tests!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 02:01:32 2025 UTC