Bug #79689 [NEW]: Casting a non-existent array index to int returns 0 instead of null
| From: | sebastian dot awatramani at gmail dot com | Date: | Thu, 11 Jun 2020 15:44:44 +0000 |
| Subject: | Bug #79689 [NEW]: Casting a non-existent array index to int returns 0 instead of null | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-227420@lists.php.net to get a copy of this message | ||
From: sebastian dot awatramani at gmail dot com
Operating system: Linux Mint 19
PHP version: 7.4Git-2020-06-11 (Git)
Package: Unknown/Other Function
Bug Type: Bug
Bug description:Casting a non-existent array index to int returns 0 instead of null
Description:
------------
Using the null coalesce operator while casting a result to int, I expect
that if the array index I'm trying to assign doesn't exist, it should
fail and then move on to the next part of the NC statement, like so:
$a = (int) $_GET['field'] ?? null
If there is no $_GET['field'] nor a $_GET in general, I expect $a to
equal null. But instead $a ends up as 0 because casting a non-existent
array index to int returns the int val of false
Test script:
---------------
$a = (int) $_GET['field'] ?? null
Expected result:
----------------
$a === null
Actual result:
--------------
$a === 0
--
Edit bug report at https://bugs.php.net/bug.php?id=79689&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79689&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79689&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79689&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79689&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79689&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79689&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79689&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79689&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79689&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79689&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79689&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79689&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79689&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79689&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79689&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79689&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79689&r=mysqlcfg