你能在PHP中获得一个变量名作为字符串吗? (你应该吗?)

你能在PHP中获得一个变量名作为字符串吗?  (你应该吗?)

问题描述:

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

假设您有几个大型变量数组(通常是字符串),并且您想要更改它们的显示方式 在某些页面上 - 例如通过将每个页面与 $ prefix code>和 $ suffix code>连接起来: p>

  $ arr = array  ($ foo,$ bar,$ baz)
 
 $ foo_display = $ prefix。  $ foo。  $ suffix 
 $ bar_display = $前缀。  $ bar。  $ suffix 
 $ baz_display = $前缀。  $ baz。  $ suffix 
  code>  pre> 
 
 

您如何避免手动完成所有这些分配? 我原先假设会有一些函数将变量的名称作为字符串返回(称之为“varname()”),在这种情况下代码可能如下所示: p>

  foreach($ arr as& $ value){
 $ {varname($ value)。“_ display”} = $ prefix。  $ value。  $ suffix 
} 
  code>  pre> 
 
 

但是我找不到这样的功能,并且人们在这个类似的主题似乎认为整个概念都是可疑的。 p>

PS:我是编程新手,对不起,如果这是一个愚蠢的问题:) p> div>

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;

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.

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.