php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46154 Un expexted result of empty string key of array
Submitted: 2008-09-22 20:50 UTC Modified: 2008-09-22 22:11 UTC
From: akam at akameng dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Windows XP SP2
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: akam at akameng dot com
New email:
PHP Version: OS:

 

 [2008-09-22 20:50 UTC] akam at akameng dot com
Description:
------------
due to this line of php manual:
"Using the empty string as a key will create (or overwrite) a key 
with the empty string and its value;"
but empty string key will work.

Reproduce code:
---------------
<?php
$test = array('zero' => 'php.net', '' => 'php.net', "" => 'php.net' );

//test
echo $test['zero']."\n<br />"; //output: php.net
echo $test['']."\n<br />";      //output: php.net
echo $test[""]."\n<br />";     //output: php.net

foreach ($test as $key => $value ){
        echo "\$key = $key and \$value = $value \n<br />";
}
//out put
/*
$key = zero and $value = php.net 
$key = and $value = php.net
*/
?>

Expected result:
----------------
php.net



php.net






Actual result:
--------------
php.net
php.net
php.net

php.net
php.net





Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-22 21:01 UTC] akam at akameng dot com
Actual result:
--------------
php.net 
php.net 
php.net 
$key = zero and $value = php.net 
$key = and $value = php.net
 [2008-09-22 22:11 UTC] tularis@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This is completely expected.
First of all, '' === "", seriously, there is 0 difference between those two. Next, I think you misunderstood the manual. It just says you CAN use an empty string, and it WILL work. If an empty string is already defined as a key, it will simply overwrite it.

$test = array('zero' => '1', '' => '2', "" => '3' );

echo $test['zero']."\n<br />"; //outputs: 1
echo $test['']."\n<br />";      //outputs: 3
echo $test[""]."\n<br />";     //outputs: 3

'' = empty string
"" = exact same empty string
you could even use a heredoc to make such an empty string. They are all exactly the same. Read up on single and double quotes to find out how they differ.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 20:01:32 2025 UTC