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

Construct a PHP variable name based on other variable values and static text

I want to tell my function which variable to call based on the day of the week. The day of the week is stored in $s_day, and the variables I want to call changes based on which day it is. e.g. I've stored a string 'Welcome to the week' in…
jkramp
  • 491
  • 1
  • 5
  • 8
3
votes
2 answers

Using Variable for Property Name of Object - Javascript

saw a few answers related to this, but none answer this version of the subject in question. Consider the following: (linkto: jsfiddle) $(function(){ arrKeys = []; objArr = []; nameArr = ['name1','name2','name3','name4']; descArr =…
Fezzik
  • 45
  • 1
  • 3
3
votes
2 answers

Assign value to variable private static class property that is an array from inside class definition

I would like to access and assign values to private static class properties and I would like to do the assigning using the concept of 'variable variables'. Accessing works, but assigning does not work. I have tried the following: class AClass { …
DudeOnRock
  • 3,685
  • 3
  • 27
  • 58
3
votes
2 answers

def'ine a value with a dynamic name

So I want to pass a function to the "name" portion of def. The problem is: "First argument to def must be a Symbol" I'm trying to for instance do: (def serverNumber 5) (def (str "server" serverNumber) {:id serverNumber :value 4939}) But I can't…
Riveascore
  • 1,724
  • 4
  • 26
  • 46
3
votes
1 answer

Variable Variables in PHP from required / included files

I am writing a MVC Framework (for the purpose of learning and discovery as opposed to actually intending to use it) and I have came across a slight problem. I have a config.php file: $route['default'] = 'home'; $db['host'] =…
Andrew Willis
  • 2,289
  • 3
  • 26
  • 53
3
votes
2 answers

Getting reference to array element where array is accessible as $obj->$propName

Suppose that we have this code (simplified example): $propertyName = 'items'; $foo = new \stdClass; $foo->$propertyName = array(42); At this point I 'd like to write an expression that evaluates to a reference to the value inside the array. Is it…
Jon
  • 428,835
  • 81
  • 738
  • 806
2
votes
4 answers

How would I call a method from a class with a variable?

Given this class: class Tacobell{ public function order_taco(){ echo "3 Tacos, thank you."; } public function order_burrito(){ echo "Cheesy bean and rice, please"; } } $lunch = new…
Ryan Florence
  • 13,361
  • 9
  • 46
  • 63
2
votes
3 answers

PHP: setting session variables through variable variables

I would like to set a session variable with something akin to: $key = '_SESSION[element]'; $$key = 'value'; This does indeed set $_SESSION['element'] equal to value, but it also seems to clear the rest of my $_SESSION variable, resulting in the…
Mala
  • 14,178
  • 25
  • 88
  • 119
2
votes
2 answers

How can I print $title1 $title2 $title3... using a for loop in PHP

I want to print these variables using a for loop:
user918672
  • 21
  • 1
2
votes
1 answer

Trying to access $_SERVER(or any global) variable from string name

Today I met such a terrible situation. It seems this bug is related to PHP. I'm trying to access to $_SERVER or another super global variables but from string name. This version of implementation is working. var_dump(${"_SERVER"}); // working But…
2
votes
1 answer

Variable Variables with array element values syntax

I want to create a session variable but the session variable name I want to be dynamic. So what I need is the proper syntax for a variable name that is a $_SESSION variable. I have tried the code below which creates a variable variable name and…
Dave Lovely
  • 67
  • 1
  • 7
2
votes
4 answers

PHP Globals access issue when using a variable variable

I have this line in a class function: $this_value = eval("return $$existing_value;"); This gives me the value I need when the $$existing_value variable is set in the function, but I've found that I actually need to access the global scope in 99% of…
YsoL8
  • 2,186
  • 5
  • 29
  • 47
2
votes
6 answers

php get the variable name from other variable

look at this simple script please $c1 = $_GET[c1]; $c2 = $_GET[c2]; $c3 = $_GET[c3]; $c4 = $_GET[c4]; $c5 = $_GET[c5]; for($i = 1;$i <=5;$i++) { echo $c{$i};//or something else here :/ } how can i print tha values of variables? Thanks
Simon
  • 22,637
  • 36
  • 92
  • 121
2
votes
1 answer

How to reach variable variables which has spaces?

I was curious about how to reach variable variables which contain spaces or special chars. Is there a way to do ? "; $days = 'saturday sunday'; $$days = 'days…
mirza
  • 5,685
  • 10
  • 43
  • 73
2
votes
2 answers

PHP - Difference in creating variable variables with brackets or double dollar signs?

From what I can tell in the PHP Manual, it doesn't seem like there is much difference between defining a variable variable with double brackets or double dollar signs. $foo = 'hello'; $$foo = 'hi'; echo $hello; // 'hi' $baz = 'goodbye'; ${$baz} =…
Katrina
  • 1,922
  • 2
  • 24
  • 42
1 2
3
11 12