Questions tagged [variable-variables]

Language feature of PHP: A variable variable takes the value of a variable and treats that as the name of a variable.

Language feature of PHP: A variable variable takes the value of a variable and treats that as the name of a variable.

171 questions
541
votes
17 answers

How do I create variable variables?

I know that some other languages, such as PHP, support a concept of "variable variable names" - that is, the contents of a string can be used as part of a variable name. I heard that this is a bad idea in general, but I think it would solve some…
Pyornide
126
votes
9 answers

"Variable" variables in JavaScript

I know it's possible in PHP to have "variable" variables. For example, $x = "variable"; $$x = "Hello, World!"; echo $variable; // Displays "Hello, World!" Is it possible to refer to a variable by its name as a string in JavaScript? How would it be…
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
53
votes
5 answers

How do I import variable packages in Python like using variable variables ($$) in PHP?

I want to import some package depending on which value the user chooses. The default is file1.py: from files import file1 If user chooses file2, it should be : from files import file2 In PHP, I can do this using variable variables: $file_name =…
skargor
  • 1,011
  • 3
  • 15
  • 21
15
votes
5 answers

How can I select a variable by (string) name?

I want to return a pre-determined list from my function, based on the string input. def get_ext(file_type): text = ['txt', 'doc'] audio = ['mp3', 'wav'] video = ['mp4', 'mkv'] return # what do I return here? get_ext('audio') …
Ludoloco
  • 177
  • 1
  • 5
14
votes
3 answers

what is "$$" in PHP

I saw this code if (is_null($$textVarName)) { $$textVarName = $_defaultTexts[$type]; } what is code "$$" ?
meotimdihia
  • 4,191
  • 15
  • 49
  • 69
12
votes
1 answer

How do I dynamically create the variable name in a PHP loop?

Ok so i have this php foreach loop rows as $step) { ?> and $step will be the step numbers 1, 2, 3, 4, 5 up to the total steps within the loop i a need to set the value of the images within the loop to standard_image_1…
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
9
votes
3 answers

java String to class

I have got a bean class named Bean1. In my main method I have got a string containing the name of the variable: String str= "Bean1"; Now how can I use the String variable to get the class and access the Bean properties?
MANU SINHA
  • 111
  • 1
  • 1
  • 3
9
votes
7 answers

What's an actual use of variable variables?

Variable variables seem pretty cool, but I can't think of a scenario where one would actually use them in a production environment. What would such a scenario be? How were they used?
user151841
  • 17,377
  • 29
  • 109
  • 171
8
votes
7 answers

How to check if a string can be used as a variable name in PHP?

In PHP one can use variable variables... For example... class obj { } $fieldName = "Surname"; $object = new obj(); $object->Name = "John"; $object->$fieldName = "Doe"; echo "{$object->Name} {$object->Surname}"; // This echoes "John Doe". However,…
Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96
6
votes
2 answers

Is the dollar sign in a variable variable considered the dereference operator?

I was showing someone how you can create variable variable variables in PHP (I'd only recommend using them NEVER, it's horrible practice and you are a bad person if you use variable variable variables in actual production code), and they asked if…
Cyclone
  • 17,939
  • 45
  • 124
  • 193
6
votes
4 answers

Variable variables handling order: changes in PHP 7

With the new PHP 7.0.0 out now, I'm a bit worried about the changes in evaluation order of the so-called 'variable variables'. On this page, under 'Changes to variable handling', a table is displayed with examples of expressions with their handling…
Marten Koetsier
  • 3,389
  • 2
  • 25
  • 36
6
votes
8 answers

Variable variable class extensions in PHP--is it possible?

Is something like the following possible in PHP? $blah = 'foo1'; class foo2 extends $blah { //... } class foo1 { //... } This gives an error. I want to dynamically set $blah so I can extend whatever class I want. Edit: The reason for…
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
6
votes
4 answers

Javascript's equivalent to PHP's $$varName

Possible Duplicate: How to access javascript variable value by creating another variable via concatenation? In PHP I can have: $theVariable = "bigToe"; $bigToe = "is broken"; such that: echo "my ".$theVariable." ".$$theVariable; would…
Aaron Luman
  • 635
  • 1
  • 10
  • 29
5
votes
1 answer

variable-variables in PHP

I know you can do: $hash('foo') and $$foo and also $bar[$foo], what are each of these things called?
Johnny
  • 1,963
  • 4
  • 21
  • 24
5
votes
3 answers

set variable variable as array key

I'm not sure how to ask this question, but here goes: $id = 1; $age = 2; $sort = "id"; // or "age"; $arr = array($$sort => 'string'); // so that it becomes 1 => 'string' ^ what goes here? // instead of 'id' => string' Is…
d-_-b
  • 21,536
  • 40
  • 150
  • 256
1
2 3
11 12