Php 动态变量
Php Dynamic Variable
$X['high'] = 1234;
$var = array("X","high");
这是有效的:
$temp = $$var[0];
$temp = $temp[$var[1]];
echo $temp;
但这不起作用:
echo $$var[0][$var[1]];
为什么?我怎样才能让它发挥作用?
您应该向 php 解析器解释您希望如何解析此语句:
echo ${$var[0]}[$var[1]];
没有括号你将有:
php7
Notice: Array to string conversion in /in/cvZqc on line 5
Notice: Undefined variable: Array in /in/cvZqc on line 5
php5
Warning: Illegal string offset 'high' in /in/cvZqc on line 5
Notice: Array to string conversion in /in/cvZqc on line 5
$X['high'] = 1234;
$var = array("X","high");
这是有效的:
$temp = $$var[0];
$temp = $temp[$var[1]];
echo $temp;
但这不起作用:
echo $$var[0][$var[1]];
为什么?我怎样才能让它发挥作用?
您应该向 php 解析器解释您希望如何解析此语句:
echo ${$var[0]}[$var[1]];
没有括号你将有:
php7
Notice: Array to string conversion in /in/cvZqc on line 5
Notice: Undefined variable: Array in /in/cvZqc on line 5
php5
Warning: Illegal string offset 'high' in /in/cvZqc on line 5
Notice: Array to string conversion in /in/cvZqc on line 5