php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20865 array_key_exists(NULL, $myarray) generates warning and returns FALSE;
Submitted: 2002-12-06 11:14 UTC Modified: 2002-12-06 11:43 UTC
From: grubby_d at yahoo dot com Assigned:
Status: Closed Package: Unknown/Other Function
PHP Version: 4.2.3 OS: XP/IIS
Private report: No CVE-ID: None
 [2002-12-06 11:14 UTC] grubby_d at yahoo dot com
this is an self explanatory bug.

RUN THIS SCRIPT

<?php

$myarray = array(1, 2, 3);

$myarray[NULL] = "the null value";

print_r($myarray); //output includes [] => "the null value"

// can reference the null indexed value without problems

//The bug is in this function:
$result = array_key_exists(NULL, $myarray) //generates warning and returns FALSE;

?>

I found the bug on redhat 7.3/apache.1.3.x with PHP verison 4.1.X but i reproduced the same behaviour on the submitted platform (PHP4.2.3) to comply with this bug report form.




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-06 11:16 UTC] grubby_d at yahoo dot com
this is an self explanatory bug.

RUN THIS SCRIPT

<?php

$myarray = array(1, 2, 3);

$myarray[NULL] = "the null value";

print_r($myarray); //output includes [] => "the null value"

// can reference the null indexed value without problems

//The bug is in this function:
if (array_key_exists(NULL, $myarray))
{
   echo "you won't see this output<BR>\n";
}
//generates warning and returns FALSE

?>

I found the bug on redhat 7.3/apache.1.3.x with PHP verison 4.1.X but i
reproduced the same behaviour on the submitted platform (PHP4.2.3) to
comply with this bug report form.
 [2002-12-06 11:39 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 [2002-12-06 11:41 UTC] msopacua@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

NULL as a key, is an empty string. NULL as an argument to a function is 'no argument', so this is expected.
If you test for array_key_exists('', $myarray) it's ok.
See here:
<?php

$myarray = array(1, 2, 3);

$myarray[NULL] = "the null value";
$myarray[''] = "the empty string value";

var_dump($myarray); //output includes [] => "the null value"

// can reference the null indexed value without problems

//The bug is in this function:
$result = array_key_exists('', $myarray); //generates warning and
var_dump($result);
?>

array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [""]=>
  string(22) "the empty string value"
}
bool(true)


 [2002-12-06 11:43 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

It was a bug and it is now fixed.
if var_dump($myarray[NULL]); is able to retrieve the data so should array_key_exists().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 02 14:01:29 2024 UTC