在PHP中,$还有其他用途吗?为什么这段代码不会产生错误?

rat*_*oss 5 php

在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!";

?>
Run Code Online (Sandbox Code Playgroud)

......和输出:

a lot of money will make me crazy - and PHP too!<hr />
My ASCII art is not a text or a comment! First time!
Run Code Online (Sandbox Code Playgroud)

p.s*_*w.g 6

PHP有一个" 变量变量 " 的概念,它允许您通过它的名称动态引用变量.例如:

$a = 'foo';
$b = 'a';
$c = 'b';
$d = 'c';

echo $ $ $ $d; // foo
Run Code Online (Sandbox Code Playgroud)

当你在作业中使用它时?希望这有助于展示一点:

$a = 'foo';
$$a = 'bar';

echo $foo; // bar
Run Code Online (Sandbox Code Playgroud)

所以在这段代码中:

echo $ $ $ $ $ $ $what_the_hell_php = 'what is wrong with you PHP?'
Run Code Online (Sandbox Code Playgroud)

要分配值,引擎将获取当前存储的值$what_the_hell_php,然后获取存储在具有该名称的变量中的值,然后获取存储在具有该名称的变量中的值,依此类推.当然,在您的示例中,$what_the_hell_php最初为null,因此实际上无法取消引用这些变量.但是,赋值表达式的结果仍然是一个值,就像所有赋值表达式一样:

echo $a = $b = $c = 'foo'; // foo
Run Code Online (Sandbox Code Playgroud)