Heim  >  Artikel  >  Backend-Entwicklung  >  php中 $$str 中 "$$" 的解释

php中 $$str 中 "$$" 的解释

WBOY
WBOYOriginal
2016-08-08 09:21:411020Durchsuche

这种写法称为可变变量
有时候使用可变变量名是很方便的。就是说,一个变量的变量名可以动态的设置和使用。一个普通的变量通过声明来设置,例如:
$a = "hello";
?>
一个可变变量获取了一个普通变量的值作为这个可变变量的变量名。在上面的例子中 hello 使用了两个美元符号($)以后,就可以作为一个可变变量的变量了。例如:
$$a = "world";
?>
这时,两个变量都被定义了:$a 的内容是“hello”并且 $hello 的内容是“world”。因此,可以表述为:
echo "$a ${$a}";
?>
以下写法更准确并且会输出同样的结果:
echo "$a $hello";
?>
它们都会输出:hello world。
要将可变变量用于数组,必须解决一个模棱两可的问题。这就是当写下 $$a[1] 时,解析器需要知道是想要 $a[1] 作为一个变量呢,还是想要 $$a 作为一个变量并取出该变量中索引为 [1] 的值。解决此问题的语法是,对第一种情况用 ${$a[1]},对第二种情况用 ${$a}[1]。

以上就介绍了php中 $$str 中 "$$" 的解释,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP之冒泡排序Nächster Artikel:PHP面试题遇到的几个坑。面壁ing