|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-06-11 15:44 UTC] 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 20:00:01 2025 UTC |
You are looking for; $a = (int) ($_GET['field'] ?? null); Casts are high-precedence operators.@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).