php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60039 array_key_exists behaves different from isset for float keys
Submitted: 2011-10-11 12:18 UTC Modified: 2021-02-16 16:07 UTC
Votes:4
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: nikic@php.net Assigned: nikic (profile)
Status: Closed Package: Arrays related
PHP Version: 5.4.0beta1 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nikic@php.net
New email:
PHP Version: OS:

 

 [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.


Patches

array_key_exists_patch.txt (last revision 2011-10-11 12:19 UTC by nikic@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-11 12:19 UTC] nikic@php.net
The following patch has been added/updated:

Patch Name: array_key_exists_patch.txt
Revision:   1318335549
URL:        https://bugs.php.net/patch-display.php?bug=60039&patch=array_key_exists_patch.txt&revision=1318335549
 [2011-10-11 19:14 UTC] cataphract@php.net
-Type: Bug +Type: Feature/Change Request
 [2011-10-11 19:14 UTC] cataphract@php.net
It's very arguable whether the behavior of the two should match.

A argument for the current behavior would go like this: in $a = [4 => "hhh"], the key 4.5 doesn't exist, so key_exists should return false; on the other hand $a[4.5] exists and is not null, so isset should return true. There are some problems with this argument, namely that key_exists actually rejects the arguments (it sends a warning) and that there is a very slight mismatch between isset and array dereferencing, but it's just to show this is not a bug (violation of well established specifications), but instead a feature request that merits more discussion.
 [2011-10-11 20:18 UTC] nikic@php.net
@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.
 [2014-03-06 13:07 UTC] narf at devilix dot net
+1

The only times when I'd want to use array_key_exists() is when I can't use isset() because it returns FALSE for NULL values.
 [2021-02-16 16:07 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2021-02-16 16:07 UTC] nikic@php.net
This has been fixed in PHP 8.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 15:01:30 2024 UTC