PHP - 如何在循环中创建变量名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17135192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - How Can I Create Variable Names Within a Loop?
提问by Jon Lachonis
My goal is to have a loop where variable names are created. Such as:
我的目标是创建一个循环来创建变量名。如:
$variable1
$variable2
$variable3
$variable1
$variable2
$variable3
and assigned values within the loop. For instance, a for loop
from 1 to 7 produces the variables $variable1, $variable2, $variable3,
.etc, each with the value of the iterator.
并在循环内赋值。例如,afor loop
从 1 到 7 产生变量$variable1, $variable2, $variable3,
.etc,每个变量都有迭代器的值。
回答by Reactgular
There are several ways. One is via a string and a double $
, but I don't like this method. A programmer could easily remove the double $
(seeing it as a typeo).
有几种方法。一种是通过 string 和 double $
,但我不喜欢这种方法。程序员可以轻松地删除 double $
(将其视为一种类型)。
for($i = 0; $i <= 7; $i++) {
$name = "variable{$i}";
$$name = "foo";
}
The best approach is the explicit statement.
最好的方法是显式声明。
for($i = 0; $i <= 7; $i++) {
${"variable$i"} = "foo";
}
With the explicit inline string there is no doubt about the intent of what you're doing. Another PHP programmer will not alter the code by mistake.
使用显式内联字符串,您所做的事情的意图是毫无疑问的。另一个 PHP 程序员不会错误地更改代码。
回答by kvsm
You should use an array for this:
您应该为此使用一个数组:
for($i = 1; $i <= 7; $i++) {
$variable[$i] = $i;
}
Edit:to clarify, you should use an array because there's no code (to my knowledge) which will accept $variable1 but not $variable[1]. Dynamically generating variable names is all kinds of wrong.
编辑:澄清一下,你应该使用一个数组,因为没有代码(据我所知)接受 $variable1 但不接受 $variable[1]。动态生成变量名是各种各样的错误。
回答by IMSoP
Just to expand on what others have said about why this is a technique best avoided: variable names exist solely for use by the programmer; they have no relationships or properties according to the language's runtime. As such, a collection of related variables should be put into a variable representing some appropriate container - in the case of PHP, its incredibly flexible "array" type.
只是为了扩展其他人所说的为什么这是一种最好避免的技术:变量名称仅供程序员使用;根据语言的运行时,它们没有关系或属性。因此,应该将一组相关变量放入一个代表某个适当容器的变量中——在 PHP 的情况下,它是非常灵活的“数组”类型。
Whenever the issue of dynamically named variables is raised, it is usually because somebody thinks it is a solution to a particular problem, but it generally leads to more problems than it solves - for instance, once part of a program starts dynamically naming variables, everywhere that wants to use those variables now alsohas to dynamically name them; finding out which items are actually set becomes unnecessarily complex, and so on.
每当提出动态命名变量的问题时,通常是因为有人认为它是对特定问题的解决方案,但它通常导致的问题比它解决的要多——例如,一旦程序的一部分开始动态命名变量,无处不在想要使用这些变量的现在也必须动态命名它们;找出实际设置的项目变得不必要地复杂,依此类推。
There are a very few contexts where variable-variables are useful, for certain kinds of debugging and meta-programming, for example, but like goto
, their presence in the language should not mean you don't try every other possible solution first.
在极少数情况下,变量变量是有用的,例如,对于某些类型的调试和元编程,但就像goto
,它们在语言中的存在不应该意味着您不会首先尝试所有其他可能的解决方案。
999 times out of 1000, though, if you find yourself wondering how to dynamically create variable names, you should reframe the question: what are you actuallytrying to achieve, and what is the best solution to that problem. If some existing / 3rd-party code has been built using this pattern (and presumably also global variables), it may be best to patch or wrap it rather than propogating the mis-design into your own code. (Admittedly, a wrapper will still need to know how to declare the dynamic variables.)
但是,1000 次中有 999 次,如果您发现自己想知道如何动态创建变量名,您应该重新思考这个问题:您实际上想要实现什么,以及该问题的最佳解决方案是什么。如果某些现有的/第 3 方代码已使用此模式(可能还有全局变量)构建,最好修补或包装它,而不是将错误设计传播到您自己的代码中。(诚然,包装器仍然需要知道如何声明动态变量。)
回答by pickman murimi
If you had an associative array i'ld prefer using the extract()
method. Check it out here
如果你有一个关联数组,我更喜欢使用这个extract()
方法。检查出来这里
for example
例如
<?php
/* Suppose that $var_array is an array*/
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape";
?>
the above code would output,
blue, large, sphere, medium
上面的代码会输出,
blue, large, sphere, medium
回答by Misters
Thats no possible since variable cannot be create in run time
那是不可能的,因为不能在运行时创建变量