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
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: 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-2021 The PHP Group
All rights reserved.
Last updated: Sat Dec 04 09:03:37 2021 UTC