Bug #79689 [Nab]: Casting a non-existent array index to int returns 0 instead of null

From: Date: Thu, 11 Jun 2020 16:33:05 +0000
Subject: Bug #79689 [Nab]: 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-227425@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 Updated by: nikic@php.net Reported by: sebastian dot awatramani at gmail dot com Summary: Casting a non-existent array index to int returns 0 instead of null Status: Not a bug 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: @bugreports: You are right. I skimmed this a bit too quickly. I believe what OP wants is just $a = isset($_GET['field']) ? (int) $_GET['field'] : null; or $a = (?int) ($_GET['field'] ?? null); with a hypothetical nullable cast operator (https://wiki.php.net/rfc/nullable-casting). Previous Comments: ------------------------------------------------------------------------ [2020-06-11 16:07:15] bugreports at gmail dot com with proper error reporting you would seewhat really happens: you are doing (int)$_GET['field'] which is afterward 0 and the ?? is a noop php > $a = (int) $_GET['field'] ?? null; var_dump($a); Notice: Undefined index: field in php shell code on line 1 > You are looking for > $a = (int) ($_GET['field'] ?? null); won't change anyhing, no way that is line ever results in expected result BULL, it will alway be int 0, one time with warnings and one time without beause in that version you cast the result of ($_GET['field'] ?? null) but you still cast the only difference is if "?? null" is executed at all ------------------------------------------------------------------------ [2020-06-11 16:00:56] brzuchal@php.net Not a bug but expected behaviour. ------------------------------------------------------------------------ [2020-06-11 16:00:33] nikic@php.net You are looking for; $a = (int) ($_GET['field'] ?? null); Casts are high-precedence operators. ------------------------------------------------------------------------ [2020-06-11 15:56:03] bugreports at gmail dot com 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 ------------------------------------------------------------------------ [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

« previous php.bugs (#227425) next »