php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45400 $foo = $foo does not work in some cases
Submitted: 2008-07-01 11:34 UTC Modified: 2008-07-01 13:00 UTC
From: php at darkk dot net dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Linux, FreeBSD
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: php at darkk dot net dot ru
New email:
PHP Version: OS:

 

 [2008-07-01 11:34 UTC] php at darkk dot net dot ru
Description:
------------
Converting from string to array using
$name['foobar'] = $name
does not work in some cases.

Reproduce code:
---------------
first:

function foobar($name) {
  $name['anything'] = $name; 
  var_dump($name); 
}
foobar("foobar");'


second:

$name = "foobar";
$name['anything'] = $name;
var_dump($name);

Expected result:
----------------
in both cases I expect to see:
array(1) {
  ["anything"]=>
  string(6) "foobar"
}


Actual result:
--------------
first:
string(6) "oobar"

second:
string(6) "foobar"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-01 11:43 UTC] php at darkk dot net dot ru
I've just found workaround for both issues:
- $name['anything'] = $name;
+ $name = array('anything' => $name);

I also can say that 5.2.0 and 5.2.3 are not affected (at least that's what people at #php @ freenode say).
 [2008-07-01 12:49 UTC] johannes@php.net
In your code:

$name = "foobar";
$name['anything'] = $name;


$name['anything'] is addressing a string offset, 'anything' is casted to 0 so you're overwriting the first character of $name with $name.

The first result, string(6) "oobar", is looking like it's overwriting the first byte with a null byte, which isn't what one expects but related to the way PHP optimizes access to variables, which isn't made for "wrong" code.
 [2008-07-01 13:00 UTC] php at darkk dot net dot ru
Thank you. I know, that test-case is weird.

I was sure it was some sort of "undefined behavior", but I saw no warnings/notices and I got two different(sic!) results for same code, so I posted the bug.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jun 18 00:01:33 2025 UTC