Heredoc PHP syntax is a way to write large bloc of text inside PHP, without the classic single quote, double quotes delimiters. It relies on <<< and a token that will also mark the end of the string.
$string = <<<STRING
Heredoc has also the Nowdoc alternative, that is the single quote version of the Heredoc. It actually has single quotes around the token definition, and will not remplace variables inside itself.
$string = <<<‘STRING’
Nowdoc is rarely used : very rarely indeed. Only 3 % of all Heredoc/Nowdoc uses the Nowdoc syntax. This may be due to the recent addition of Nowdoc (PHP 5.3), or to the classic fear of a future use that would require variables inside the string.
Top 10 tokens for heredoc
The most common token is OET, by far. It is supposed to mean ‘End Of Text’, which is always rather strange as an opening delimiter. Of course, it is much more useful at the end of the string.
Note also that EOT is the delimiter of choice of the documentation ! This is one of those occurrences where the manual has a tremendous impact on the way developers choose their tokens : it was in the manual, and so it is used.
A much more interesting usage of the Heredoc token is to give it a meaning, that is reused. PHP doesn’t care about the token itself, but two other tools do like it : IDE and static analysis. For example, when you use HTML as a token, VIM does activate the HTML syntax highlighting within this string, and it keeps PHP in the rest of the code.
Needless to say that it is also a good indication for the reader that some langage is used within this Heredoc. Here are the most common tokens that are also langages :
Heredoc’s usage tokens
Note that besides technology, some of the tokens bears the purpose of the blob of text. Those are usually text that needs to be in the code, but are not manipulated. They are very rarely used this way.
Heredoc funny tokens
Last, of course, since tokens are totally free, some of them are chosen with a lot of freedom.
|HEREDOC||39||At least, we know what it is|
|FINDCHILDREN||4||No Find Parents ?|
|BAZ||7||No foo, no bar|
|CIA||4||No FBI, no NSA|
|EOTURTLES||1||No Lion, no elephpants|
|PAPAYA||1||No Pears, no pecl|
|HUMPTYDANCE||1||Lots of imagination|
|O||12||short and sweet|
The stats were collected over 838 Open Source projects. They represent 250 millions of PHP tokens, and 14 millions lines of code. We retained 24479 Heredoc / Nowdoc syntax for this article.