Heredoc PHP and its usage

Heredoc PHP

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.

 <?php

$string = <<<STRING

$x elephpants

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.

<?php

$string = <<<‘STRING’

$x elephpants

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

 

Token Occurrences
EOT 12607
EOF 2500
EOD 1452
HTML 940
SQL 822
END 453
ICS 449
XML 243
‘EOF’ 219
EOS 205

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.

Technology tokens

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 :

Token Occurrences
HTML 940
SQL 822
XML 243
JS 114
JSON 65
CSS 50
VCARD 20

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.

Token Occurrences
HELP 61
TEMPLATE 33
LONGDESC 30
CONTENT 22
DIGEST 13
HEADER 7
LEGEND 6
NEWS 6

Heredoc funny tokens

Last, of course, since tokens are totally free, some of them are chosen with a lot of freedom.

TokenOccurrencesSarcasm

Token Occurrences Comment
BLA 40
HEREDOC 39 At least, we know what it is
BUFFER 15
details 9
FINDCHILDREN 4 No Find Parents ?
BAZ 7 No foo, no bar
CIA 4 No FBI, no NSA
helllo 2 No English
FABRICE 1 No Damien
EOTURTLES 1 No Lion, no elephpants
PAPAYA 1 No Pears, no pecl
HUMPTYDANCE 1 Lots of imagination
GOOD_JSON 1 No BAD_JSON
O 12 short and sweet

 

Annex

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.

One thought on “Fun with delimiters of Heredoc PHP

  1. Pingback: PHP Heredoc 的乐趣 - Exakat

Comments are closed.