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

Streamlit dynamic UI to generate dynamic input widgets depending on value from a different input widget

I want to open this post as I can't find anything on the official documentation from streamlit or any resources that mentioned how to do this. After some trial and error I have figured out a way, and will post the answer below. This is a function…
Subaru Spirit
  • 394
  • 3
  • 19
1
vote
3 answers

Obtaining a variable value when echoing its name inside a for loop

I'm learning and I've been stuck for so long now with something I believe is too simple, sorry if I'm right. Please help me to evolve, here's my question: I have coming from a form: $text1 = $_POST['TEXT1']; $text2 = $_POST['TEXT2']; $text3 =…
user712027
  • 572
  • 6
  • 9
  • 22
1
vote
4 answers

What does ${$item} mean?

I'm not able to understand braces goal in the situation below and I find no serious documentation about braces usage. See the example below: $var = array('a','b','c','d'); foreach($var as $item){ ${$item} = array(); } I'm non…
alesdario
  • 1,873
  • 6
  • 25
  • 38
1
vote
2 answers

PHP - Variable Variables & array_merge() - not working

I have a bunch of arrays, which are stored in different variables like $required, $reserved, etc... I would like to allow (inside a function) an array of options to be passed (like $options = array('required', 'reserved')), and that array would then…
geerlingguy
  • 4,682
  • 8
  • 56
  • 92
1
vote
3 answers

Variable variables as parameters of custom function in usort()

I've been playing around and seeing if I can set up a more dynamic method in PHP. usort( $dataset, function($a, $b){ return strcasecmp($a[$this->parameters], $b[$this->parameters]); } ); This line would sort array elements in…
1
vote
1 answer

How can I access a specific depth of an associative array with variable variables

I've been hitting my head on this problem for some time now. I am working on a piece of software that creates a tree from a MySQL result checking for changes between each row to decide where in the tree to put the new data. I'm now at a dead end…
Francesco
  • 429
  • 4
  • 12
1
vote
0 answers

Undefined variable on defining variable variables

I want to import data from a .json file into a PHP file and dynamically, by using variable variables and a foreach loop, define new variables. However, PhpStorm doesn't recognize my variables and give me Undefined variable warnings. What to…
MAChitgarha
  • 3,728
  • 2
  • 33
  • 40
1
vote
2 answers

Is Variable Variable Possible?

My googlefu has failed me and I come to you for help: Is VBA capable of having Variable Variables like PHP? I know that in PHP you can wait to declare a variable by using $$var. Is it possible to do it in VBA? for example, is there a way that lets…
Overseer10
  • 361
  • 2
  • 9
  • 20
1
vote
6 answers

php string name as variable in array

how take string from array define as new array, how to code in php $column = array("id","name","value"); let say found 3 row from mysql want result to be like this $id[0] = "1"; $id[1] = "6"; $id[2] = "10"; $name[0] = "a"; $name[1] = "b"; $name[2]…
Paisal
  • 1,311
  • 3
  • 11
  • 13
1
vote
1 answer

Are different PHP variable variables that resolve to the same variable name the same variable?

Let's say I have an array of strings that I use to create variable variables: $var_arr = ["item1", "item2", "item3"]; foreach ($var_arr as $item) { $$item = array(); } Then elsewhere in my code I have to recreate the variable variable based on…
Troy D
  • 381
  • 2
  • 4
  • 17
1
vote
6 answers

PHP - Variable variables or array for variable inside for()

I have this code that generates an HTML table with php: '; echo ''; for ($i = 1; $i <= 9; $i++) { if($a1_pos_txt !== TRUE) { echo "" . $numbers["a" . $i . "_pos"] . ""; } else…
Trufa
  • 39,971
  • 43
  • 126
  • 190
1
vote
2 answers

What happens when $$[object name] is declared?

I was trying to debug a PHP script when I came across a declaration like: $cart = new form; $$cart = $cart->function(); What is $$cart?
anand johi
  • 11
  • 1
1
vote
5 answers

javascript equivalent of php $$ dollar dollar

I have declared a local variable named cont in a function named validate. I am calling a function process from inside validate. I am sending the string 'cont' as argument to validate function. In the process function using the string 'cont' i want…
Jayapal Chandran
  • 10,600
  • 14
  • 66
  • 91
1
vote
1 answer

Use variable variables with PHP's Superglobal arrays outside of function and class

I'm beginner of PHP, I found a Warning says: Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. So, does it mean I can use variable variables with Superglobal outside of function and…
LIU YUE
  • 1,593
  • 11
  • 19
1
vote
4 answers

Using variable variables to populate array results in error

I have the following code: function filterUsers(array $setOfAllUsers) { if (empty($setOfAllUsers)) { return array(array(), array()); } $activeUsers = array(); $inactiveUsers = array(); foreach($setOfAllUsers as $userRow) { …
GGio
  • 7,563
  • 11
  • 44
  • 81