PHP 中可以获取字符串形式的变量名吗? (你应该吗?)

发布于 2024-10-02 14:47:59 字数 676 浏览 1 评论 0原文

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

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

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

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

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

但我一直无法找到这样的函数,以及这个类似帖子中的人似乎认为整个概念都是可疑的。

PS:我是编程新手,抱歉,如果这是一个愚蠢的问题:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子的影子 2024-10-09 14:47:59

您想做的事情存在多个问题。例如,变量的名称不可访问。代码 $arr = array($foo, $bar, $baz) 使用 $foo等创建一个数组。如果您随后更改了 $foo 的值,则 $arr[0] 的值仍然是 $foo 的,所以甚至不清楚访问变量名称意味着什么。

即使这很容易做到,我也会认为这是非常糟糕的做法,因为您需要通过内省知道正确的变量名称是什么。

当然,如果你有关联数组,这一切都很容易解决。例如:

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

这可以很容易地改变以产生你想要的东西。例如:

$display = array();
foreach($arr as $key => $value) 
   $display[$key] = $prefix . $value . $suffix;

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 2024-10-09 14:47:59

我需要一个更详细的示例来说明您想要完成的任务,但我给您的方向是研究 PHP 如何处理键/值对:

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

这样你就可以像这样访问你的数组:

foreach ($valsArr as $key => $val) {
    $displayX = $prefix.$superArr[$key.'_display'].$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.

时光礼记 2024-10-09 14:47:59

尽管可以从 $GLOBALS 数组派生变量名称,但当您可以创建自己的自定义关联数组来跟踪这些变量值对时,迭代变量值对没有多大意义。如果您有两个或多个具有相同内容的 $value 实例,您会如何期望程序通过内省来跟踪变量名称?很快就会变得一团糟。

您应该自己在代码中跟踪它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文