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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 06:01:31 2024 UTC