8.9 變數:進階應用

  參考資料:請點這裡
// e.g. 1

$str = 'Hello World';
echo ${str};

$str = 'Hello World';
echo $str;
// e.g. 2

$tmp = 'World';
echo "Hello {$tmp}";

$tmp = 'World';
echo "Hello ${tmp}";

$tmp = 'World';
echo "Hello $tmp";
// e.g. 3

$tmp = 'World';
$str = <<<EOF
    Hello {$tmp}
EOF;
echo $str;

$tmp = 'World';
$str = <<<EOF
    Hello ${tmp}
EOF;
echo $str;

$tmp = 'World';
$str = <<<EOF
    Hello $tmp
EOF;
echo $str;
// e.g. 4

$tmp = 'str';
$str = 'Hello World';
echo ${$tmp};

$tmp = 'str';
$str = 'Hello World';
echo $${tmp};

$tmp = 'str';
$str = 'Hello World';
echo $$tmp;
// e.g. 5

$i = 1;
while ($i <= 5) {
    ${tmp.$i} = $i;
    $i++;
}
echo $tmp5;
  您可以在 這裡 線上執行以上 PHP 程式碼,看看輸出的結果為何。