php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80978 array_key_exists() does not work for false/true
Submitted: 2021-04-23 16:48 UTC Modified: 2021-04-26 08:47 UTC
From: reed dot hi at taeluf dot com Assigned: nikic (profile)
Status: Closed Package: Reproducible crash
PHP Version: 7.4.16 OS: Fedora 33
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: reed dot hi at taeluf dot com
New email:
PHP Version: OS:

 

 [2021-04-23 16:48 UTC] 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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-26 08:47 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2021-04-26 08:47 UTC] nikic@php.net
This is supported since PHP 8.0, see https://3v4l.org/l9U34.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 08:02:42 2024 UTC