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
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,817
  • 3
  • 24
  • 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,662
  • 4
  • 16
  • 26
4
votes
3 answers

Is it bad practice to use variable variables in PHP?

For example, a simple MVC type system: /api/class/method rewritten into PHP variables using .htaccess/nginx.conf, then doing something like:
Jonathan Coe
  • 1,485
  • 4
  • 18
  • 36
4
votes
2 answers

Access Variable variables with an object

I don't really know how to decribe this problem, so I'm sorry if the title is a bit unclear. I got an object with array fields. I got the name of these fields stored in a variable and I want to reach an element in one of those array fields.…
Barthje
  • 143
  • 2
  • 7
4
votes
7 answers

How can I simplify this redundant code?

Can someone please help me simpling this redundant piece of code? if (isset($to) === true) { if (is_string($to) === true) { $to = explode(',', $to); } $to =…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
3
votes
4 answers

Unexpected behaviour with variable variables

I was trying to pass a variable that contained the name of the superglobal array I wanted a function to process, but I couldn't get it to work, it would just claim the variable in question didn't exist and return null. I've simplified my test case…
GordonM
  • 31,179
  • 15
  • 87
  • 129
3
votes
14 answers

PHP: Class property chaining in variable variables

So, I have a object with structure similar to below, all of which are returned to me as stdClass…
Matt Humphrey
  • 1,554
  • 1
  • 12
  • 30
3
votes
2 answers

Using eval() in the code, and how would you avoid it?

In my code, I want to try fetching $_POST or $_GET variables, and unsetting them as soon as they were requested once. This function returns the method used, which is simple enough. Unfortunately I cannot simply return the $_POST or $_GET variables…
Dwarf Vader
  • 429
  • 1
  • 5
  • 14
3
votes
2 answers

Variable properties on a variable method

I'm using a third-party package which manipulates images. For basic conversions it accepts a single value: $image = $this->addMediaConversion('thumb'); $image->width(100); $image->height(100); The system I am building has a level of abstraction…
Mike
  • 8,767
  • 8
  • 49
  • 103
3
votes
3 answers

Variable variables: when useful?

Possible Duplicate: What's an actual use of variable variables? OK, this question may look a little bit point-whoreish, but I'd really like to know: when are variable variables useful? I haved programmed in PHP for several years, but I've never…
fabrik
  • 14,094
  • 8
  • 55
  • 71
3
votes
1 answer

Dynamically numbered variables in foreach loop

I have an array full of sub-arrays, which in turn are full of source names and their website URLs, such as The New York Times and http://newyorktimes.com. I'm displaying a list of links where the title links to the url and I would like to show the…
Shoelaced
  • 846
  • 5
  • 27
3
votes
2 answers

How can I concatenate a string with a variable and use it as another variable?

I have a variable $language, which can be: "en", "nl", or "fr".I have 3 other variables $menu_en, $menu_fr and $menu_fr. I have a php page where I want the menu to appear in any of the 3 languages. I tried this: echo '$menu_' . $language; What I…
Paulcraey
  • 373
  • 4
  • 14
3
votes
3 answers

PHP Dynamically accessing an associative array

EDITED FROM ORIGINAL THAT HAD IMPLIED ONLY 1 ACCESS If I have an array that contains x number of arrays, each of the form array('country' => array('city' => array('postcode' => value))) and another array that might be array('country', 'city',…
3
votes
1 answer

PHP Variable object properties

I understand the concept of variable variables in PHP. According to my understanding of variable variables in PHP the following code: $foo = 'hello'; $$foo = 'world'; echo $foo . ' ' . $hello; Will generate the output as: Hello World But I am…
Rahul Bhanushali
  • 553
  • 5
  • 14
3
votes
5 answers

Variable Variables Pointing to Arrays or Nested Objects

Is it possible to create a variable variable pointing to an array or to nested objects? The php docs specifically say you cannot point to SuperGlobals but its unclear (to me at least) if this applies to arrays in general. Here is my try at the…
K-2052
  • 197
  • 4
  • 10
1
2
3
11 12