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.

165 questions
426
votes
16 answers

How do I create variable variables?

How do I accomplish variable variables in Python? Here is an elaborative manual entry, for instance: Variable variables I hear this is a bad idea in general though, and it is a security hole in PHP. Is that true?
Pyornide
109
votes
8 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,117
  • 11
  • 41
  • 65
48
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
  • 951
  • 3
  • 14
  • 21
14
votes
3 answers

what is "$$" in PHP

I saw this code if (is_null($$textVarName)) { $$textVarName = $_defaultTexts[$type]; } what is code "$$" ?
meotimdihia
  • 4,417
  • 14
  • 45
  • 65
11
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
  • 39,076
  • 79
  • 210
  • 312
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
6 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
  • 15,348
  • 28
  • 93
  • 156
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
  • 8,854
  • 14
  • 66
  • 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,178
  • 45
  • 119
  • 184
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
  • 625
  • 1
  • 10
  • 28
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,683
  • 4
  • 19
  • 22
5
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
  • 2,769
  • 2
  • 20
  • 33
5
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
  • 134,677
  • 87
  • 206
  • 257
5
votes
8 answers

When to use a variable variable in PHP?

I've been developing in PHP for a while now, and I still have not had a task where I've had to use variable variables. Can anyone give me examples where using them is a good idea ? Or were they included in the language just for fun ?
sjobe
  • 2,759
  • 2
  • 22
  • 32
4
votes
7 answers

PHP $_GET/$_POST via variable variables

I'm attempting to dynamically access both the $_GET and $_POST arrays, among others, using variable variables. The reason I'm trying to do this is so that I can perform similar actions on multiple arrays without needing to declare specific…
mseancole
  • 1,634
  • 4
  • 16
  • 24
1
2 3
10 11