What is EOT in PHP

Define strings with HEREDOC syntax (Theme: PHP examples)

1. Notice

The following article is largely the same as that about the NOWDOC syntax. If you have already read the latter, it is sufficient to skim over this roughly.

2 Introduction

HEREDOC is a special syntax in PHP for marking longer, often multiline, sections of text. They are introduced by “<<< MARKING”, whereby “MARKING” can be any string. As a rule, it is capitalized to improve clarity. “<<< MARKING” must be followed by a line break and then the text. This can be written in the same way as between double quotation marks. Variables are also automatically recognized by PHP here and replaced by their values. In contrast to double quotation marks, however, double quotation marks can also be placed within the string without them having to be escaped. The end of the HEREDOC section is marked with "MARKER;" which must be at the beginning of a new line.

In addition to the HEREDOC syntax, there is also the NOWDOC syntax, which is explained in a separate article. While variables are parsed in the strings with HEREDOC, this is not the case with NOWDOC. HEREDOC is similar to the string definition using double quotation marks, NOWDOC is more like the one using single quotation marks.

3. Example: A simple string in HEREDOC syntax

In this example, $ heredoc is defined as a string with the content “abcdefg” using the HEREDOC syntax.

PHP code <?php $heredoc = <<<STRING abcdefg STRING; var_dump($heredoc); ?>
HTML code: output string (7) "abcdefg"

4. HEREDOC with several lines

Similar to the previous example, but this time the string spans several lines.

PHP code <?php $heredoc = <<<STRING abcdefg abcdefg abcdefg STRING; var_dump($heredoc); ?>
HTML code: output string (25) "abcdefg abcdefg abcdefg"

5. HEREDOC and indentations

In this example, indents are made using tabs. As can be seen in the output, these are also adopted. If you want to avoid this, you must not indent any lines in HEREDOC.

PHP code <?php $heredoc = <<<STRING test STRING; var_dump($heredoc); ?>
HTML code: output string (6) "test"

6. HEREDOC with different start and end characters

In the next example, different names are chosen for the start and end markers. The naming follows the same rules as for variables: lower and upper case letters, numbers and underscores are allowed. However, the name cannot begin with a number.

PHP code <?php $heredoc1 = <<<X 123456789 X; var_dump($heredoc1); $heredoc2 = <<<__bla__ test123 __bla__; var_dump($heredoc2); $heredoc3 = <<<a1234 Noch ein String. a1234; var_dump($heredoc3); ?>
HTML code: output string (9) "123456789" string (7) "test123" string (16) "Another string."

7. HEREDOC with the start mark in quotation marks

Optionally, the start marker can also be placed in double quotation marks. This makes the HEREDOC syntax more similar to NOWDOC, as single quotation marks are required for the latter.

PHP code <?php $heredoc = <<<"X" asdasdasdasd X; var_dump($heredoc); ?>
HTML code: output string (12) "asdasdasdasd"

8. Access to variables in HEREDOC

Within HEREDOC, variables are automatically replaced by their values, as is known from the string definition with double quotation marks.

PHP code <?php $x = 10000; $heredoc = <<<STRING Der Wert von x ist: $x STRING; var_dump($heredoc); ?>
HTML code: output string (25) "The value of x is: 10000"

9. Use quotation marks within the text

HEREDOC shows its strengths with multi-line texts, which can also contain quotation marks. Overall, the text is easier to read than with a normal definition using single or double quotation marks.

PHP code <?php $heredoc = <<<STRING Dies ist ein "Text", der auch 'Anführungszeichen' enthält, was bei "normaler" Definition einige Schrägstriche/Backslashes zum Escapen erfordern würde. STRING; var_dump($heredoc); ?>
HTML code: output string (156) "This is a" text "that also contains 'quotation marks', which would require a few slashes / backslashes to escape with a" normal "definition."

10. The start and end markings may be included in the text

The characters from the start and end markings may be part of the text. Placement at the beginning of a line is also permitted. In the latter case, however, at least one further character must follow the character string, as it is not mandatory in HEREDOC to end the HEREDOC section with a semicolon.

PHP code <?php $heredoc = <<<X Dieser Text enthält ein X. X; var_dump($heredoc); ?>
HTML code: output string (27) "This text contains an X."

In a new line, without any further character (generates an error):

PHP code <?php $heredoc = <<<X Dieser Text enthält ein X. X X; var_dump($heredoc); ?>
HTML code: output <br /> <b>Parse error</b>: syntax error, unexpected T_STRING in <b>...\test.php</b> on line <b>5</b><br />

11. HEREDOC can lead to unsightly code

At the top or global level in the code, the HEREDOC syntax is nice to look at, but with deeper nesting it can lead to unsightly code, since the text and the end marker should or must be at the beginning of the line.

PHP code <?php class MyClass { function doSomething($a) { if ($a === true) { $str = <<<HEREDOC Dies ist ein beliebiger Text. Sogar mit mehreren Zeilen! (Und noch eine dritte Zeile.) HEREDOC; echo $str; } } } $obj = new MyClass(); $obj->doSomething(true); ?>
HTML code: output This is any text. Even with several lines! (And a third line.)

12. HEREDOC for fields of classes and class constants

Just like for the definition of "normal" variables, HEREDOC can also be used to set fields and constants of a class.

PHP code <?php class HeredocClass { public $v1 = <<<DOC Test v1 DOC; public static $v2 = <<<DOC Test v2 DOC; const v3 = <<<DOC Test v3 DOC; } $obj = new HeredocClass(); var_dump($obj->v1, $obj::$v2, $obj::v3); ?>
HTML code: output string (7) "Test v1" string (7) "Test v2" string (7) "Test v3"

13. Use HEREDOC as a parameter

Strings defined via HEREDOC do not necessarily have to be assigned to variables. Instead, it is also possible to use them directly as parameters. This is shown below by transferring a string defined via HEREDOC to echo ().

PHP code <?php echo <<<HEREDOC_MIT_ECHO Heredoc lässt sich problemlos mit echo verknüpfen, sodass der Heredoc-String direkt ausgegeben wird. HEREDOC_MIT_ECHO; ?>
HTML code: output Heredoc can easily be linked with echo so that the Heredoc string is output directly.

In this second example, a HEREDOC string is passed to var_dump, similar to the previous example.

PHP code <?php var_dump(<<<HEREDOC_MIT_VAR_DUMP Heredoc mit var_dump HEREDOC_MIT_VAR_DUMP ); ?>
HTML code: output string (21) "Heredoc with var_dump"

14. Line breaks before the end of HEREDOC

This example analyzes how HEREDOC treats line breaks directly before the end marker. To do this, three line breaks are set after the last character. As can be seen, the very last line break is truncated, the other two are retained.

PHP code <?php $str = <<<HEREDOC X HEREDOC; var_dump($str); ?>
HTML code: output string (5) "X"

15. Space before the end of HEREDOC

Analogous to the previous example, we will now examine how HEREDOC treats spaces before its end. This time, three spaces are placed before the last line break (which must be set). As can be seen in the output, HEREDOC does not remove the spaces.

PHP code <?php $str = <<<HEREDOC X HEREDOC; var_dump($str); ?>
HTML code: output string (4) "X"

16. The marking of the end must be in a new line

In this example, the end marker is not set at the beginning of a new line as a test. The result is a parse error. The end marker must always be at the beginning of a line.

PHP code <?php $str = <<<HEREDOC X HEREDOC; var_dump($str); ?>
HTML code: output <br /> <b>Parse error</b>: syntax error, unexpected $end in <b>...\test.php</b> on line <b>5</b><br />

17. HEREDOC with error suppression

If array keys are to be accessed within the HEREDOC section that are potentially not defined, warnings can be suppressed by using the @ symbol. This must be placed directly in front of the start marker (“@ <<< MARKING”). It should be noted that this is an ugly style as the warnings are still generated. The @ sign just means that they are no longer displayed.

PHP code <?php function printError($data) { echo @<<<ERR Hallo {$data['username']}, es ist ein Fehler mit folgender Nachricht aufgetreten: {$data['message']} Ort des Fehlers: {$data['file']}, Zeile {$data['line']}. ERR; } printError(array('message' => 'Ein Beispielproblem ist aufgetreten.', 'file'=>'index.php', 'line'=>127123)); ?>
HTML code: output Hello, an error occurred with the following message: An example problem has occurred. Location of the error: index.php, line 127123.

The same example without error suppression (only the @ is missing):

PHP code <?php function printError($data) { echo <<<ERR Hallo {$data['username']}, es ist ein Fehler mit folgender Nachricht aufgetreten: {$data['message']} Ort des Fehlers: {$data['file']}, Zeile {$data['line']}. ERR; } printError(array('message' => 'Ein Beispielproblem ist aufgetreten.', 'file'=>'index.php', 'line'=>127123)); ?>
HTML code: output
Notice : Undefined index: username in ... \ test.php on line 4
Hello, it is an error occurred with the following message: A sample problem has occurred. Location of the error: index.php, line 127123.