在PHP中,以下代码不会产生语法错误。作为开发人员,这会在我脑海中产生语法错误。任何线索?
<?php
$ $ $ $ $ $ $what_the_hell_php = 'what is wrong with you PHP?';
echo $what_the_hell_php; // no output
echo $ $ $ $ $ $ $what_the_hell_php; // worth a try but no output too
// echo $; // well, don't do this. this produces an actual PHP syntax error.
$dollars = 'a lot of money will make me crazy';
echo $dollars;
echo $$$$$$$$$$$$$$$$$$$$$$$$$$ $lotsofmoney = " - and PHP too!";
// echo $$$$$$something $hello = 'hello'; // won't work, PHP likes pure dollars
// is it for this??
echo
$$$ $$$$$$ $$$$$$ $$$$ $$$$ $$$ $$$$$$$$ $$$$$$$$
$$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$
$$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$
$$ $$ $$$$$$ $$ $$ $$ $$ $$ $$$$$$$$ $$
$$$$$$$$$ $$ $$ $$ $$ $$$$$$$$$ $$ $$ $$
$$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$
$$ $$ $$$$$$ $$$$$$ $$$$ $$$$ $$ $$ $$ $$ $$
$ascii = "<hr />\nMy ASCII art is not a string or a comment! First time!";
?>
...和输出:
a lot of money will make me crazy - and PHP too!<hr />
My ASCII art is not a text or a comment! First time!
答案 0 :(得分:6)
PHP有一个&#34; variable variables&#34;的概念。它允许您通过它的名称动态引用变量。例如:
$a = 'foo';
$b = 'a';
$c = 'b';
$d = 'c';
echo $ $ $ $d; // foo
当你在作业中使用它时?希望这有助于展示一点:
$a = 'foo';
$$a = 'bar';
echo $foo; // bar
所以在这段代码中:
echo $ $ $ $ $ $ $what_the_hell_php = 'what is wrong with you PHP?'
要分配值,引擎将获取当前存储在$what_the_hell_php
中的值,然后获取存储在具有该名称的变量中的值,然后获取存储在具有该名称的变量中的值,依此类推。当然,在您的示例中,$what_the_hell_php
最初为null,因此它实际上无法取消引用这些变量。但是,赋值表达式的结果仍然是一个值,就像所有赋值表达式一样:
echo $a = $b = $c = 'foo'; // foo