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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
29 + 16 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 22:01:27 2024 UTC