简体   繁体   English

php数字变量名称

[英]php numeric Variable Name

I want to append some $variables automaticly and set their names numeric I have a script look like this: 我想自动附加一些$ variables并将它们的名称设置为数字,我有一个脚本,如下所示:

<?php 
$i=0;
while($i<=100){

$variable_[$i]=$i;
$i++;
#with "[$i]" I mean their name will be $variable_1 , $variable_2, $variable_3 ... 
#they will be automatic increased variables non manual!

}
?>

This is called variable variables . 这称为变量变量

You can set a variable variable by defining its name inside a variable, such as: 您可以通过在变量内定义其名称来设置变量变量,例如:

$name = 'variable_' . $i;

and then assign a value to it by doing: 然后通过执行以下操作为其分配值:

$$name = $i;

Note that variable variables can easily be misused. 请注意,变量变量很容易被滥用。 Make sure you completely understand the repercussions of this feature on your code and the risk of having bugs, and ensure this is the only solution you have, ie you can't use an array ( $variables[$i] = $i; ) instead. 确保您完全了解此功能对代码的影响以及存在错误的风险,并确保这是唯一的解决方案,即不能使用数组( $variables[$i] = $i; )代替。

Its better to use Array with key=> value pair. 最好将Array与key => value对一起使用。 You can build this array dynamically and then loop through it by using foreach. 您可以动态构建此数组,然后使用foreach遍历它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM