#46596 [Ver->Tbd]: Setting an associative array $var with the key $var: $var[$var] = true
| From: | jani@php.net | Date: | Wed, 28 Jan 2009 23:06:48 +0000 |
| Subject: | #46596 [Ver->Tbd]: Setting an associative array $var with the key $var: $var[$var] = true | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-1768@lists.php.net to get a copy of this message | ||
ID: 46596
Updated by: jani@php.net
Reported By: benjaminNOpickSPAM at web dot de
-Status: Verified
+Status: To be documented
-Bug Type: Arrays related
+Bug Type: Documentation problem
Operating System: *
PHP Version: 5CVS, 6CVS (2008-11-24)
New Comment:
You're quite right there. Reclassified.
Previous Comments:
------------------------------------------------------------------------
[2008-11-24 19:48:43] lbarnaud@php.net
Jani, this seems to be an expected behavior. The code basically does
$var[0] = "1";
And $var becomes "1xxxx".
------------------------------------------------------------------------
[2008-11-18 23:31:33] jani@php.net
This is just side-effect of the fact that [] is also used for
strings. It's not just for arrays.
------------------------------------------------------------------------
[2008-11-17 17:20:40] benjaminNOpickSPAM at web dot de
Description:
------------
As the following lines are ok:
$var = $var + 1;
$var[trim($var2)] = '';
I expected PHP to first evaluate the $key-String ([$var]), then modify
$var. So a line like:
$var = 'a';
$var[$var] = 0;
should work.
Reproduce code:
---------------
<?php
$var = 'xxxxx';
$var[$var] = true;
var_dump($var);
?>
Expected result:
----------------
array(1) { ["xxxxx"]=> bool(true) }
or a warning like:
Warning: Cannot use a scalar value as an array
Actual result:
--------------
string(5) "1xxxx"
Depending on what code was before it, or what was in $var before, the
first character is not always '1'.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46596&edit=1