Heredocs - Using same name twice? Why name them at all?
Toying with heredocs in PHP, I realized the name of the heredoc does not have to be unique. Thus:
$a = <<开发者_如何学Go;<EOD
Some string
EOD;
$b = <<<EOD
A different string
EOD;
is correct and behaves exactly as you would expect.
Is this bad practice for any reason? Why does a heredoc need a name/label (EOD
above) at all, since you can't reference it by the name?
What if the string you're specifying contains EOD
?
You can select the identifier to avoid conflicts with the chunk of text you're using as a string.
You don't reference it as such, but it acts as an identifier to indicate the end of the heredoc. e.g.
$a = <<<EOD
EOA
EOB
EOC
EOD;
One benefit is that editors like vim can apply syntax highlighting to heredocs named with HTML, EOHTML, EOSQL, EOJAVASCRIPT making them much prettier to work with...
$html = <<<EOHTML
<p class="foo">foo</em>
EOHTML;
$sql = <<<EOSQL
SELECT DISTINCT(name) FROM foo ORDER BY bar;
EOSQL;
$js = <<<EOJAVASCRIPT
foo = { bar: 'bar'; }
EOJAVASCRIPT;
精彩评论