Doc #75801 [NEW]: Mistake in documentation example for null coalesce operator
| From: | pieter at frenssen dot be | Date: | Thu, 11 Jan 2018 13:23:31 +0000 |
| Subject: | Doc #75801 [NEW]: Mistake in documentation example for null coalesce operator | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-15328@lists.php.net to get a copy of this message | ||
From: pieter at frenssen dot be
Operating system:
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:Mistake in documentation example for null coalesce operator
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';
}
?>
--
Edit bug report at https://bugs.php.net/bug.php?id=75801&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75801&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75801&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75801&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75801&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75801&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75801&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75801&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75801&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75801&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75801&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75801&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75801&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75801&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75801&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75801&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75801&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75801&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75801&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75801&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75801&r=mysqlcfg