php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26747 Variable Variables don't work properly with Arrays
Submitted: 2003-12-30 04:26 UTC Modified: 2003-12-30 05:24 UTC
From: japril78 at hotmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.4 OS: WinXP Pro
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: japril78 at hotmail dot com
New email:
PHP Version: OS:

 

 [2003-12-30 04:26 UTC] japril78 at hotmail dot com
Description:
------------
The does not work as expected:

$array = array('one','two','three');
$arraypath = 'array[1]';
echo ${$arraypath};

Workaround:

$array = array('one','two','three');
$arraypath = 'myarray[1]';
eval('$value = $'.$arraypath.';');
echo $value;

BUT:

The workaround DOES NOT work inside a function.

Reproduce code:
---------------
$array = array('one','two','three');
$arraypath = 'array[1]';
$value = showit($arraypath);

function showit($arraypath)
{
    eval('$value = $'.$arraypath.';');

    echo $value;        // Attempt #1
    echo $$arraypath;   // Attempt #2
    echo ${$arraypath}; // Attempt #3
}

Expected result:
----------------
nothing

Actual result:
--------------
nothing

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-30 04:27 UTC] japril78 at hotmail dot com
Description:
------------
This does not work as expected:

$array = array('one','two','three');
$arraypath = 'array[1]';
echo ${$arraypath};

Workaround:

$array = array('one','two','three');
$arraypath = 'myarray[1]';
eval('$value = $'.$arraypath.';');
echo $value;

BUT:

The workaround DOES NOT work inside a function.

Reproduce code:
---------------
$array = array('one','two','three');
$arraypath = 'array[1]';
$value = showit($arraypath);

function showit($arraypath)
{
    eval('$value = $'.$arraypath.';');

    echo $value;        // Attempt #1
    echo $$arraypath;   // Attempt #2
    echo ${$arraypath}; // Attempt #3
}

Expected result:
----------------
nothing

Actual result:
--------------
nothing
 [2003-12-30 04:29 UTC] japril78 at hotmail dot com
Description:
------------
This does not work as expected:

$array = array('one','two','three');
$arraypath = 'array[1]';
echo ${$arraypath};

Workaround:

$array = array('one','two','three');
$arraypath = 'array[1]';
eval('$value = $'.$arraypath.';');
echo $value;

BUT:

The workaround DOES NOT work inside a function.

Reproduce code:
---------------
$array = array('one','two','three');
$arraypath = 'array[1]';
$value = showit($arraypath);

function showit($arraypath)
{
    eval('$value = $'.$arraypath.';');

    echo $value;        // Attempt #1
    echo $$arraypath;   // Attempt #2
    echo ${$arraypath}; // Attempt #3
}

Expected result:
----------------
nothing

Actual result:
--------------
nothing
 [2003-12-30 05:24 UTC] eru@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

Well, what did you expect? [1] is not part of the variables name, ${$arraypath}[1] works just fine.
Secondly it is no wonder, that the function won\'t echo the value, because $array is not defined in that scope.

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jan 05 05:01:28 2025 UTC