PHP 字符串中的 Heredoc 和 Nowdoc

首先要了解下定界符

php可以使用

<?php
echo <<<EOT
this is body
this is body as well
EOT
;

這種方式來輸出大段文字,其中EOT就是定界符,定界符有分為開始定界符和結束定界符,定界符可以是任何內容,只要保證開始定界符和結束定界符一致就行,比如

<?php
echo <<<AAA
this is body
this is body as well
AAA
;

這就是heredoc的寫法,heredoc中的變量會被解析成具體的值

<?php
$a = 'this is string';
echo <<<AAA
this is body
this is body as well
$a
AAA
;
// output
this is body
this is body as well
this is string

如果希望內容不被解析,可以在開始定界符的兩邊加上單引號'EOT'

<?php
$a = 'this is string';
echo <<<'AAA'
this is body
this is body as well
$a
AAA
;
// output
this is body
this is body as well
$a

這就是nowdoc的寫法

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容