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
1
vote
1 answer

Dynamically append indexes to variable

So I have arrays of different lengths pathes= array(array('f1e9'), array('c0d9', '0', 'form_values', '6e13') ); For each path I would like to use each value as an index to scan a separate array. foreach ($pathes as $key => $val){ …
ambe5960
  • 1,870
  • 2
  • 19
  • 47
1
vote
1 answer

Variable Variables and Sums of Them in Excel VBA

To alleviate my summer boredom, I decided to try to make an Excel program that keeps track of how much money someone would owe based on properties owned and number of houses on those properties in the board game Monopoly (some new rules I'm…
Raman
  • 13
  • 4
1
vote
1 answer

Can't pass a variable variable into a function in PHP since upgrading from 5.5.22 to 5.6.6?

This line of code worked perfectly before I upgraded, now it doesn't: 123: $thisRow[] = displayR(${"sR{$i}S1"}, ${"nR{$i}S1P"}); I get this error: Undefined variable: sR1S1 in script.php on line 123 A workaround is to manually assign them first $s…
Codemonkey
  • 4,455
  • 5
  • 44
  • 76
1
vote
1 answer

JavaScript variable variables

Say I have object: function obj() { this.prop1; this.prop2; this.prop3; } and an array of obj's objects = [new obj(),new obj(),new obj()]; I want to easily iterate through each using jquery where the class name is equivalent to the…
Jeff
  • 480
  • 1
  • 5
  • 17
1
vote
1 answer

How do I create a variable moustache variable?

Take this code for example: title: 'Hello', The variable is called title and the value is hello. I would be able to call it via {{ title }} and get the result.However, recently I needed to create a variable variable. I do not want the name of the…
Max
  • 2,710
  • 1
  • 23
  • 34
1
vote
2 answers

Looping variable variables, illegal string offset

I keep getting Warning: Illegal string offset 'type' in ... on line ... I've tried following the answers here Illegal string offset Warning PHP by doing something like if(isset($_POST['type_'.$i])) $$T['type'] = $_POST['type_'.$i]; but it…
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
1
vote
2 answers

Variable variables in JavaScript?

In PHP I can mess around with variable variables and I'm wondering if I can do the same in JavaScript. I want to create a new object with a property which's name is based on the value of a variable. if ( obj.name === 'foo' ) { var data = { foo:…
Till
  • 1,107
  • 12
  • 28
1
vote
2 answers

Variable variable not working

I have some code that doesn't seem to work. I'm using a form to post a value (green, blue, red, etc) and make it look up something in the database using that post value as part of a variable name. $mine = 'green'; $querymine =…
s1h4d0w
  • 762
  • 6
  • 27
1
vote
1 answer

PHP use key to get an element of an array that uses variable as array name

Below is the code sample: $test = array( 1 => 'one', 2 => 'two', 3 => 'three' ); $arrayName = 'test'; error_log(print_r($$arrayName, 1)); error_log($$arrayName[1]); The output: Array ( [1] => one [2] => two [3] =>…
aarryy
  • 502
  • 5
  • 20
1
vote
1 answer

PHP get_called_class() as variable for referencing static property

I'm trying every variation of the following to refer to a static property: get_called_class()::$$prop I've tried this: ${get_called_class()}::$$prop I've tried many things, but can't seem to get it. I know I can just do this: $className =…
OCDev
  • 2,280
  • 3
  • 26
  • 37
1
vote
2 answers

foreach loop result assign to progressing variable

I have a form that includes 4 text fields and multiple file upload (5 files/fields): The database table has 9 fields, 4 for the text fields and the remaining 5 should hold a URL path to the images being uploaded by the form, for the 5 URL fields in…
DMSJax
  • 1,709
  • 4
  • 22
  • 35
1
vote
2 answers

Indexing into an array returned by a variable variable in PHP

I'm just wondering about how variable variables that point to Arrays handle. Take the code below: $a = new stdClass(); $a->b = array('start' => 2); $c = 'b'; $d = 'start'; print $a->$c; // => Array print $a->$c['start']; // => Array print…
EdgeCaseBerg
  • 2,761
  • 1
  • 23
  • 39
1
vote
2 answers

variable variables bad practice to use?

I have just read the post is it bad practice to use variable variables in php in the following fashion? explaining why they are bad to use in with classes however, i have to create dynamic variables to be sorted for example: $array = array( …
Liam Sorsby
  • 2,912
  • 3
  • 28
  • 51
1
vote
5 answers

Check if variable name in Post array contains text using PHP

I have a form that allows the user to add more fields if they need them. When the user does this, two more fields appear (one for link text, the other for the link address). I'm use javascript to append a number onto the input names (e.g.…
Sg1456
  • 954
  • 2
  • 10
  • 17
1
vote
3 answers

php - use variable variable in while loop

I'm a bit confused about variable variables. What I like to do is print the value of $var1, $var2 and $var3: $var1 = 'a'; $var2 = 'b'; $var3 = 'c'; $i = 1; while ( $i <=3 ) { echo $var.$i; $i++; } I know $var.$i; is not correct, but I…
Robert
  • 118
  • 1
  • 10