Bug #79689 [Com]: Casting a non-existent array index to int returns 0 instead of null
| From: | bugreports at gmail dot com | Date: | Thu, 11 Jun 2020 15:56:03 +0000 |
| Subject: | Bug #79689 [Com]: Casting a non-existent array index to int returns 0 instead of null | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-227421@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79689&edit=1
ID: 79689
Comment by: bugreports at gmail dot com
Reported by: sebastian dot awatramani at gmail dot com
Summary: Casting a non-existent array index to int returns 0
instead of null
Status: Open
Type: Bug
Package: Unknown/Other Function
Operating System: Linux Mint 19
PHP Version: 7.4Git-2020-06-11 (Git)
Block user comment: N
Private report: N
New Comment:
please fix your expectations according how ternary operator works for many years now, your expected
behavior would it make completly useless for 99% of all use cases
Previous Comments:
------------------------------------------------------------------------
[2020-06-11 15:44:44] sebastian dot awatramani at gmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=79689&edit=1