Doc #80978 [Opn->Csd]: array_key_exists() does not work for false/true
| From: | nikic@php.net | Date: | Mon, 26 Apr 2021 08:47:28 +0000 |
| Subject: | Doc #80978 [Opn->Csd]: array_key_exists() does not work for false/true | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-18723@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80978&edit=1
ID: 80978
Updated by: nikic@php.net
Reported by: reed dot hi at taeluf dot com
Summary: array_key_exists() does not work for false/true
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Reproducible crash
Operating System: Fedora 33
PHP Version: 7.4.16
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This is supported since PHP 8.0, see https://3v4l.org/l9U34.
Previous Comments:
------------------------------------------------------------------------
[2021-04-23 16:48:05] reed dot hi at taeluf dot com
Description:
------------
---
From manual page: https://php.net/function.array-key-exists
---
array_key_exists() returns false when checking for a key that is boolean false or true, yet the
value can be accessed by $thearray[false] & array_keys($thearray) includes the boolean
true/false
null is not int|string, but works without issue, so I think that part is a documentation issue.
Test script:
---------------
$data = [false=>'happy', null=>'sad', true=>'also happy'];
var_dump(array_key_exists(true, $data)); // false, PHP Warning
var_dump(array_key_exists(false, $data)); // false, PHP Warning
var_dump(array_key_exists(null, $data)); // true, No Warning
var_dump($data[false]); // happy
var_dump(
in_array(false,array_keys($data))
); // true
var_dump(
in_array(true,array_keys($data))
); // true
var_dump(
in_array(null,array_keys($data))
); // true
Expected result:
----------------
var_dump output:
true
true
true
"happy"
true
true
true
(and no errors)
Actual result:
--------------
var_dump output:
false
false
true
"happy"
true
true
true
PHP Warning: array_key_exists(): The first argument should be either a string or an integer
PHP Warning: (same warning again)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80978&edit=1