|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-10-11 12:18 UTC] nikic@php.net
Description: ------------ array_key_exists allows only string, int and null keys. For everything else it'll throw an error. isset on the other hand allows floats, resources and bools too. floats are cast to int and resources and bools are handled as ints. array_key_exists behavior should match isset behavior. Patchesarray_key_exists_patch.txt (last revision 2011-10-11 12:19 UTC by nikic@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 14:00:01 2025 UTC |
@Artefacto Yes, I know that it isn't perfectly obvious that the behavior should match. But I'll try to explain why I think so: a) From the user perspective: What would be a typical application of array_key_exists? Probably it's checking whether a key exists before using it: if (array_key_exists($key, $array)) { $array[$key]; } What would you expect from this code? I would expect that the if is evaluated for every key that $array[$key] won't throw an "Undefined index" notice for. For example (why I actually opened the bug) Twig does array_key_exists checks when accessing array offsets and people have reported bugs about it throwing errors when used with have whole-number float arguments (like 14.0). b) From the PHP perspective: It's arguable whether in [4 => "hhh"] the key 4.1 exists or not. I mean, you can access [4 => "hhh"][4.1], so in some way it does exist. c) We have a precedent: array_key_exists was already modified in the past to support NULL arguments just like isset() does. I don't see why we can't do that for the other argument types.