Search code examples
phpdynamic-variables

Php Dynamic Variable


$X['high'] = 1234;
$var = array("X","high");

This is working:

$temp = $$var[0];
$temp = $temp[$var[1]];
echo $temp;

But this isn't working:

echo $$var[0][$var[1]];

Why? How can i make it works?


Solution

  • You should explain to php parser how you want this statement to be parsed:

    echo ${$var[0]}[$var[1]];
    

    Without brackets you will have:

    php7

    Notice: Array to string conversion in /in/cvZqc on line 5

    Notice: Undefined variable: Array in /in/cvZqc on line 5

    php5

    Warning: Illegal string offset 'high' in /in/cvZqc on line 5

    Notice: Array to string conversion in /in/cvZqc on line 5

    Sample link.