[英]PHP How do I read the form variables with empty square brackets in their name
[英]How do I name these php variables
我想使用一个forloop来创建多个var ...它们的名称是$var1
到$var15
。 我怎么做? 我正在执行$$var.$i
但无法正常工作。
for($i=1; $i <= 15; $i++){
$$var.$i = 'this is the content of var'.$i;
}
如果您确实想这样做,则必须执行${$var . $i}
${$var . $i}
。 现在,它被解释为($$var).$i
。
但是,这是非常糟糕的风格。 相反,您应该使用数组[ php docs ]来存储值:
$var = array();
for($i=1; $i <= 15; $i++){
$var[$i] = 'This is the content of $var['.$i.'].';
}
var_export($var);
输出:
array ( 1 => 'This is the content of $var[1].', 2 => 'This is the content of $var[2].', 3 => 'This is the content of $var[3].', 4 => 'This is the content of $var[4].', 5 => 'This is the content of $var[5].', 6 => 'This is the content of $var[6].', 7 => 'This is the content of $var[7].', 8 => 'This is the content of $var[8].', 9 => 'This is the content of $var[9].', 10 => 'This is the content of $var[10].', 11 => 'This is the content of $var[11].', 12 => 'This is the content of $var[12].', 13 => 'This is the content of $var[13].', 14 => 'This is the content of $var[14].', 15 => 'This is the content of $var[15].', )
这样更有效,通常更安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.