0
$arr[0]=123;
$a="arr[0]";
echo $$a;

gives me error

Notice: Undefined variable: arr[0]

on the last line.

What should I do to make it work?


EDIT:

Above is the simplification of what I want to do. If someone wants to know why I want to do this, then here's the explanation:

This is something like what I want to do:

if(condition){
    $a=$arr1[0][0];
    $b=$arr1[0][1];
    $c=$arr1[0][2];
}
else{
    $a=$arr2[0];
    $b=$arr2[1];
    $c=$arr2[2];
}

I can compact it like this:

if(condition)
    $arr=$arr1[0];
else
    $arr=$arr2;
$a=$arr[0];
$a=$arr[1];
$a=$arr[2];

But I wanted to try doing this using variable variable:

if(condition)
    $arr="$arr1[0]";
else
    $arr="$arr2";
$a={$$arr}[0];
$b={$$arr}[1];
$c={$$arr}[2];

Sure, we don't need variable variables as we can still code without them. I want to know, for learning PHP, why the code won't work.

8
  • 3
    You don't need variable variables Commented May 5, 2013 at 18:45
  • I need them to compact the code. Commented May 5, 2013 at 18:49
  • 2
    compact the code ?? you must be joking right ? Commented May 5, 2013 at 18:50
  • 7
    If you want to compact it, use echo 123;. Commented May 5, 2013 at 18:51
  • 1
    I guess you mean that it should be $a = $arr[0] ? and then echo $a ? Commented May 5, 2013 at 18:55

4 Answers 4

1

Now that you said what you’re actually trying to accomplish: Your code doesn’t work because if you look at $arr1[0][0], only arr is the variable name; the [0] are special accessors for certain types like strings or arrays.

With variable variables you can only specify the name but not any accessor or other operation:

A variable variable takes the value of a variable and treats that as the name of a variable.

Your solution with the additional variable holding the array to access later on would be the best solution to your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Now that you said what you’re actually trying to accomplish my initial code was enough to convey the problem.
1

What you are trying to do just won't work - the code $arr[0] is referencing a variable called $arr, and then applying the array-access operator ([$key]) to get the element with key 0. There is no variable called $arr[0], so you cannot reference it with variable-variables any more than you could the expression $foo + 1 .

The real question is why you want to do this; variable variables are generally a sign of very messy code, and probably some poor choices of data structure. For instance, if you need to select one of a set of variables based on some input, you probably want a hash, and to look up an item using $hash[$item] or similar. If you need something more complex, a switch statement can often cover the cases you actually need.

If for some reason you really need to allow an arbitrary expression like $arr[0] as input and evaluate it at runtime, you could use eval(), but be very very careful of where the input is coming from, as this can be a very easy way of introducing security holes into your code.

Comments

1

FROM PHP DOC

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Use

echo ${$a}[0]; // 123 

Edit : Based on your edit you can simply have

list($a, $b, $c) = (condition) ? $arr1[0] : $arr2;

Or

$array =  (condition) ? $arr1[0] : $arr2;
$a = $array[0];
$b = $array[1];
$c = $array[2];

Comments

0

As pointed out you don't need variable variables. To get a PHP variable variable name containing index (a key) use array_keys() or array_search() or other array parsers. From php's site:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;

You could also use the following (using $var= instead of echo):

$arr[0]=123;
$arr[1]=456;
foreach ($arr as $key => $value) {
    echo "arr[{$key}] = {$value} \r\n";
}

Which outputs:

arr[0] = 123
arr[1] = 456

But I don't see why you'd do that, since the whole point of the array is not doing that kind of stuff.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.