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 :)