What is the use of <<<EOD in PHP?

What is the use of <<<EOD in PHP?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The <<<EOD Heredoc Syntax in PHP: A Deep Dive

When working with libraries like TCPDF for generating PDFs from Drupal nodes, you often encounter code snippets that look like this:

$html = <<<EOD
        <tr>
          <td>TEST</td>
        </tr>
EOD;

If you omit the <<<EOD tag or use it incorrectly, libraries expecting raw HTML strings will throw errors. This is not just a quirky syntax feature; it is a deliberate design choice in PHP to handle multi-line string literals without the overhead of opening and closing quotes. Understanding how this operator works under the hood prevents subtle bugs and saves you from debugging whitespace issues or unexpected variable interpolation.

How the Syntax Works

The <<< operator introduces what is known as a Heredoc (or "here-document"). Unlike standard double-quoted strings, which require a closing quote on the same line as the opening quote, a Heredoc allows you to define a string across multiple lines using a delimiter.

In the example above, <<<EOD starts the string. The content between this line and the final EOD; is treated as a literal block of text. The critical rule here is that the terminating delimiter (EOD) must appear at the very beginning of the line, with no leading whitespace (tabs or spaces) before it. If you indent the closing tag, PHP will treat that line as part of the string content rather than the end of the block.

Controlling Variable Interpolation

One of the most significant differences between standard strings and Heredocs is how they handle variables. By default, Heredocs behave like double-quoted strings: variables are interpolated. If you define a variable named $content before running this code, PHP will automatically replace $content inside the block with its value.

$name = 'John';
$html = <<<EOD
Welcome, $name!
EOD; // Result: "Welcome, John!"

However, if you need to treat the entire block as a literal string—where variables should remain untouched and be escaped by the library later—you must use a Nowdoc. A Nowdoc is syntactically identical to a Heredoc but uses <<<='DELIMITER' (note the equals sign) or simply defines it without variable interpolation.

$name = 'John';
$html = <<<'EOD'
Welcome, $name!
EOD; // Result: "Welcome, $name!"

For PDF generation libraries, this distinction is often critical. Some libraries expect raw HTML tags and CSS without interpreting PHP variables within the string definition. If you accidentally use a standard Heredoc when you need a Nowdoc, your PDF might render blank or corrupted sections because the library tries to parse $var as an HTML entity rather than literal text.

Trade-offs and Common Pitfalls

While Heredocs offer a clean way to write HTML or SQL queries in PHP, they come with specific trade-offs. The primary advantage is readability; you don't have to escape newlines or use "\n" characters. However, the requirement for strict indentation on the closing delimiter can lead to "off-by-one" errors where the string includes an unintended newline or a leading space.

Another common mistake involves mixing indentation styles. If your HTML content has significant indentation (tabs or spaces) for readability, ensure that the terminating EOD; is strictly flush left. If you accidentally add a tab before EOD, PHP will include that whitespace in the final string. For libraries like TCPDF that are sensitive to exact byte counts, this extra whitespace can shift the rendering engine's internal calculations, leading to layout breaks.

Furthermore, while Heredocs are convenient for static blocks of code, they do not support dynamic delimiters based on user input. If you were to build a template engine where the delimiter itself is part of the content (e.g., using <<< as part of the string), you would hit a syntax error because PHP must see the closing token immediately after the newline.

When to Use Heredocs vs. Concatenation

For simple strings, concatenation with single quotes remains the standard. However, for large HTML blocks containing CSS classes, nested tags, or multi-line SQL queries, Heredocs reduce cognitive load and improve maintainability. The decision often comes down to whether you need variable substitution. If you do not need variables inside the string, consider using a Nowdoc (<<<'EOD') to enforce literal behavior by default, which can prevent subtle bugs when passing data to strict parsers like PDF generators.

By understanding the nuances of <<<EOD versus <<<='EOD', and adhering to strict formatting rules for the closing delimiter, you ensure that your multi-line strings are passed exactly as intended to downstream libraries.

Our network