Understanding the Difference Between heredoc and nowdoc

Heredoc and nowdoc are PHP syntax features that help in creating multi-line strings.

Heredoc works similarly to double-quoted strings, which means it allows for variable interpolation and recognizes special characters. In contrast, nowdoc behaves like single-quoted strings, treating the content literally without expanding any variables.

Both syntaxes start with <<<, write your content over as many lines as you need, and then close it with a matching identifier. The difference comes in how PHP processes the content.

Heredoc

Heredoc works much like a double-quoted string, allowing for variable parsing. This means that a variable will be replaced with its value inside the block.

This is ideal when you want to integrate dynamic data, such as in an HTML email template, or a database query.

Variables get parsed, so in place of $name, we get its value inside the block.

Nowdoc

Nowdoc, on the other hand, treats everything literally. What you write is exactly what you get. It works like single-quoted strings; it’s great for code samples, raw SQL snippets, or any content where you want variable names to appear exactly as they are.

Now we get: “Hello, $name! Your order has been confirmed.”

Notice the quotes around 'EOT‘ in the opening line — that’s the only visible difference in syntax, and it’s what tells PHP to skip all parsing.

Understand Identifiers

In our previous examples, the identifier EOT is just a convention — it stands for “End of Text,” but PHP doesn’t assign it any special meaning. You can use virtually any identifier you want, as long as you follow a few basic rules.

The identifier must:

  • Start with a letter or underscore (no numbers first)
  • Contain only letters, numbers, and underscores
  • Match exactly between the opening and closing line. They must have the same case and spelling. Opening with <<<Html and closing with html will throw a parse error

So all of these are perfectly valid:

In practice, using a descriptive identifier is the better habit. <<<HTML tells other developers what kind of content resides inside the block. <<<EOT works fine, but it carries no context.

The same rules apply to nowdoc.

When do you use which?

The key point to think about is whether this string requires variables.

If the answer is yes, heredoc is the ideal choice.

If not, you should chose nowdoc.

Nowdoc is safe for user-generated content that you can’t control, as it doesn’t involve parsing, eliminating the risk of unintended variable substitution.

Note: Both syntaxes need the closing identifier to be on its own line, without any leading spaces or tabs (before PHP 7.3). Starting from version 7.3, you can use indentation, but the identifier must remain consistent. If you get this wrong, PHP will throw a parse error.

Wrapping Up

Heredoc and nowdoc solve the same problem of messy and difficult-to-read string handling, but they do so in different ways. One parses the string, while the other does not.

This simple difference is crucial for making the right choice. Understanding when to use each is a valuable skill that sets apart well-organized code from disorganized one.

Although my blog doesn’t support comments, feel free to reply via email or X.