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
7 answers

How can I use a variable variable in PHP?

Here is what I am trying to do: The above code prints '$my_str' to the screen. But I want it to print 'My String', as in the actual…
AnonPHP
0
votes
1 answer

OO PHP returning all private variables as variables on page

I have the following class, which has a lot of private variables. class plantOfTheMonth { //Declare which centre(s) are being used private $centre = ""; //Declare the attributes of the current Plant Of The Month private $name = ""; private…
Phil Young
  • 1,334
  • 3
  • 21
  • 43
0
votes
2 answers

Using variable variables in simple PHP function

The other day I was given this huge set of arrays and told to make a HTML page with a bunch or selects/radio buttons so I wrote some simple functions to simplify things. This is my function to generate very large HTML selects. function…
Spunkie
  • 137
  • 6
0
votes
0 answers

How can I echo a php variable that's INSIDE a php string variable?

I've got a database full of text fields, all of which contain WITHIN them the characters $myvariable. When I retrieve such text field and save it as a php variable, say as $myDBtext, I can definitely echo it, but I'm having really hard time echoing…
0
votes
3 answers

Displaying Dynamic Variable Value

I have a form that will ultimately create a pipe delimited text file. The form contains multiple rows each row has multiple drop down fields named 1_cat[], 2_cat[], 3_cat[] etc. In the form submission script I have: if ($submit) { …
MarkHFLA
  • 107
  • 1
  • 10
0
votes
2 answers

How to generate properties of instances of classes in batches?

I have a class named MyClass. I want to generate the attributes k_10sec to k_1d of the instance in batches when generating instances of the class. How do I modify the code? bars = ['10sec', '1min', '5min', '30min', '1d'] class MyClass: def…
jaried
  • 632
  • 4
  • 15
0
votes
2 answers

PHP variable variable not overriding original variable

I am trying to override some variables in my PHP file using variable variables, but I am getting unexpected results, where the original value is never overwritten, but where the new variable-variable value has a unique value from that of its…
Brandon McConnell
  • 5,776
  • 1
  • 20
  • 36
0
votes
1 answer

How do I create a dynamic variable based on the row number I am iterating over in an Excel sheet while using openpyxl?

I am trying to iterate over all the rows in an Excel worksheet and set a variable based on the cell that is currently being iterated over. Below is the code that I am using: import openpyxl from openpyxl import load_workbook wb =…
ak40837
  • 13
  • 1
  • 6
0
votes
3 answers

How to check if function exists inside another function using Javascript

I`m trying to build an function that load script on-demand. That is my current code: function loadScript(src, name = null) { var dfd = jQuery.Deferred(); if (name === null) { name = src.split(/\\|\//); // split by folder separator …
0
votes
4 answers

php variable variables and array or straight code?

I am using code igniter. I have quite a large list of items being sent through post, some will be set, some wont be. I have two ways of approaching the same problem. /*DYNAMIC CODING */ $fields = array( 'field1', 'field2', 'field3', 'field4', …
Hailwood
  • 89,623
  • 107
  • 270
  • 423
0
votes
1 answer

Variable Variable in django template

view.py variable = 'amount' amount = 200 print(variable) #actual: 'amount' expected: 200 In such case, is it possible to use Variable Variable like PHP? in php, I could do $variable = 'amount'; $amount = 200; echo $$variable; I know it might be…
Wils
  • 1,178
  • 8
  • 24
0
votes
1 answer

How would I use the contents of one variable to create the name of another?

I'm creating code that creates new user identifications for users, and then adds all their account details to a dictionary and then adds this dictionary to another dictionary of all the accounts. My problem is that I need each of these 'accounts',…
0
votes
1 answer

Passing variable variable into closure throws a parse error?

I just noticed that if you try to put a variable variable into a use list of a closure, it throws a parse error. Example code: foreach ($array as $key => $item) { $$key = $item->something; $this->doSomething($key, function ($param) use…
aborted
  • 4,481
  • 14
  • 69
  • 132
0
votes
3 answers

can array values be accessed by variable variables?

I have an array which I can only access correctly via variable variables, like so: $foo['bar'] = "pie"; $fixed_name_variable = "foo['bar']"; echo $$fixed_name_variable; Which in theroy echo's pie. Except it's just not returning anything. So I…
YsoL8
  • 2,186
  • 5
  • 29
  • 47
0
votes
2 answers

How can I make a variable's name from another variable AND string?

Say I had the variable $foo which has a value of "bar" and I wanted to make a variable variable from it, but also append the string "123" to it, so that the variable name is $bar123. How would I do this? I already know that $$foo = "abc123" would…
David Wheatley
  • 426
  • 6
  • 16