1

Say you have several large arrays of variables (which generally are strings), and you want to change how they are displayed on certain pages – for example by concatenating each of them with $prefix and $suffix:

$arr = array($foo, $bar, $baz)

$foo_display = $prefix . $foo . $suffix
$bar_display = $prefix . $bar . $suffix
$baz_display = $prefix . $baz . $suffix

How would you avoid having to make all these assignments manually? I originally assumed there would be some function which would return a variable's name as a string (call it "varname()"), in which case the code might look like this:

foreach ($arr as &$value) {
    ${varname($value)."_display"} = $prefix . $value . $suffix
}

But I haven't been able to find such a function, and people in this similar thread seemed to think the entire concept was suspect.

PS: I'm new to programming, sorry if this is a dumb question :)

Community
  • 1
  • 1
Zach
  • 1,291
  • 2
  • 13
  • 21
  • possible duplicate of [How to get a variable name as a string in PHP?](http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php) – Daniel A. White Nov 15 '10 at 02:10
  • This isn't a duplicate – it's an entirely different use case. Both that poster and myself are considering the same strategy (getting variable names as strings), but I'm also asking for input on whether a different strategy entirely is more appropriate to what I'm trying to accomplish. – Zach Nov 15 '10 at 02:17
  • No reliable way. If you want it really hard, you could try to make $value an `SplString` (php 5.4) and use __toString and debug_backtrace() magic to deduce an original variable name. However your case has the extra problem of using "$value". Which if there was such a language feature, would be what a varname() lookup should return; despite the reference. So, interesting topic but little hope for a solution. Go with `new StringWrapper($str,"name")` if it's an application necessity. – mario Nov 15 '10 at 02:27

3 Answers3

4

There are multiple problems with what you want to do. For example, the name of the variable is not accessible. The code $arr = array($foo, $bar, $baz) creates an array with the values of the $foo, etc. If you then changed the value of $foo, the value of $arr[0] is still the old value of $foo, so it's not even clear what it would mean to have access to the variables name.

Even if it were easy to do, I would consider it very poor practise, simply because you'd need to know by introspection what the correct variable names would be.

Of course, this is all easily solved if you had an associative array. For example:

$arr = array('Foo' => $foo, 'Bar' => $bar, 'Baz' => $baz)

This could easily be altered to produce what you want. For example:

$display = array();
foreach($arr as $key => $value) 
   $display[$key] = $prefix . $value . $suffix;
Hamish
  • 22,860
  • 8
  • 53
  • 67
0

I would need a more detailed example of what you are trying to accomplish, but the direction I would give you is to look into how PHP does key/value pairs:

http://php.net/manual/en/language.types.array.php

This way you can access your arrays like this:

foreach ($valsArr as $key => $val) {
    $displayX = $prefix.$superArr[$key.'_display'].$suffix;
}

This will have to be tailored to your specific structures/mappings. Going the route of trying to use variable names to do any sort of mappings is going to be tough.

wajiw
  • 12,239
  • 17
  • 54
  • 73
0

Although one can derive the variable name from the $GLOBALS array, iterating over the variable-value pairs just doesn't make much sense when you can create your own custom associative array to track these variable-value pairs anyway. And if you had two or more instances of $value having the same content, how would you expect a program to trace the variable name thru introspection? It can soon be a big mess.

You should keep track of it in the code yourself.

bcosca
  • 17,371
  • 5
  • 40
  • 51