Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any situation where an array is not suitable but a variable variable is? I cannot think of one or find one on any Stack Programmer Q&A.

<?php

$foo = array();
for($i = 0;$i < 3; $i++){

    $foo[$i] = "bar ".$i;


}

var_dump($foo); // ["bar 0", "bar 1", "bar 2"]

for($i = 0;$i < 3; $i++){

    $foo = "bar".$i;

    $$foo = "baz ".$i;


}

echo $bar0; //baz 0
echo $bar1; //baz 1
echo $bar2; //baz 2

?>
share|improve this question

closed as unclear what you're asking by MichaelT, Bart van Ingen Schenau, gnat, GlenH7, Snowman Feb 24 at 4:08

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
The only reasonable use I can think of for variable variables is templating. Templating is a form of metaprogramming for which variable variables seem uniquely suitable. See ejeliot.com/blog/101 – Robert Harvey Feb 19 at 22:36
1  
I think you are misunderstanding what arrays and variables are meant to represent. I think the only proper answer is "use them more and see which is easier" because some things need to be experienced rather than explained. – user40980 Feb 20 at 14:16
    
Understood, I guess I just need to come across that situation where I need to use it. The question itself was more of a response to the amount of questions on SO where people ask how to use VVs when they should be using an array. – Adam Copley Feb 20 at 14:58
up vote 2 down vote accepted

I cannot think of a reason or specific case. The only example I have (one implementation - not mine) is in an OOP approach where you may build classes/ objects dynamically and use $$vars.

Personally, I don't like my code/ interfaces to be this open. I tend to use arrays if I am unsure of my properties and then once I know what I need, make them individual properties.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.