Heredoc and Nowdoc
What Are Heredoc and Nowdoc?
In simple terms: Heredoc and nowdoc are ways to write long, multi-line strings without wrapping them in quotes. You mark the start and end with a name of your choice, and everything in between becomes the string.
Technically:
- Heredoc (
<<<IDENTIFIER) behaves like a double-quoted string: variables are interpolated and escape sequences such as\nand\tare processed. - Nowdoc (
<<<'IDENTIFIER') behaves like a single-quoted string: nothing is interpolated and no escape sequences are processed — the text is taken exactly as written.
Why They Exist
Long text in ordinary quotes quickly becomes hard to manage:
<?php
$name = "Anu";
$html = "<div class=\"card\">\n <h2 class=\"title\">Welcome, $name</h2>\n <p>Thanks for joining.</p>\n</div>";
Every inner double quote must be escaped, line breaks are written as \n, and the structure is hard to see. With heredoc:
<?php
$name = "Anu";
$html = <<<HTML
<div class="card">
<h2 class="title">Welcome, $name</h2>
<p>Thanks for joining.</p>
</div>
HTML;
The text looks exactly like the output. Quotes need no escaping, and line breaks are real line breaks.
Typical uses:
- HTML fragments and email templates
- SQL queries
- Plain-text emails, reports, and command-line help messages
- JSON or XML samples in tests
- Generated code or configuration files
Heredoc Syntax
Structure
$variable = <<<IDENTIFIER
Text line 1
Text line 2 with $variables
IDENTIFIER;
| Part | Rule |
|---|---|
<<< | Starts the heredoc |
IDENTIFIER | Any name following variable naming rules (letters, digits, underscores; not starting with a digit). By convention, uppercase. |
| Body | Starts on the line after the opening identifier |
| Closing identifier | The same name, on its own line (it may be indented — see below) |
The identifier can also be written in double quotes (<<<"IDENTIFIER"), which behaves identically to the unquoted form.
Example
<?php
$customer = "Ramesh";
$orderId = 7781;
$items = 3;
$message = <<<TEXT
Hello $customer,
Your order #$orderId with $items items has been shipped.
Track it any time from your account page.
Regards,
GoShop Team
TEXT;
echo $message;
Output
Hello Ramesh,
Your order #7781 with 3 items has been shipped.
Track it any time from your account page.
Regards,
GoShop Team
How It Works
<<<TEXTbegins the string. The string content starts on the next line.- Variables (
$customer,$orderId,$items) are interpolated, just as in double quotes. - Blank lines and line breaks are preserved exactly.
- The line containing only
TEXT;ends the string. The semicolon belongs to the PHP statement ($message = ...;), not to the heredoc. - The newline directly before the closing identifier is not part of the string. The value ends with
GoShop Team, without a trailing newline.
Interpolation and Escapes in Heredoc
All interpolation forms from double-quoted strings work in heredoc.
<?php
$user = ['name' => 'Kala', 'points' => 1250];
$date = new DateTime('2026-10-08');
echo <<<TEXT
Name:\t{$user['name']}
Points:\t{$user['points']}
Date:\t{$date->format('d/m/Y')}
Price shown as: \$99 "including tax"
TEXT;
Output
Name: Kala
Points: 1250
Date: 08/10/2026
Price shown as: $99 "including tax"
{$user['name']}and{$date->format(...)}use the curly-brace syntax.\tbecomes a tab.\$produces a literal dollar sign.- Double quotes need no escaping.
Function calls and constants are not interpolated (the same limitation as double-quoted strings), so prepare such values in variables first.
Nowdoc Syntax
Structure
$variable = <<<'IDENTIFIER'
Raw text — nothing is processed
IDENTIFIER;
The only difference from heredoc is the single quotes around the opening identifier.
Example
<?php
$name = "Kala";
echo <<<'CODE'
<?php
$name = "World";
echo "Hello, $name!\n";
CODE;
Output
<?php
$name = "World";
echo "Hello, $name!\n";
$name is not replaced (even though a $name variable exists), and \n stays as a backslash followed by n. Nowdoc is the right choice whenever the text itself contains $, backslashes, or code — such as code samples in documentation, shell scripts, or regular expressions.
Heredoc vs Nowdoc
| Feature | Heredoc <<<ID | Nowdoc <<<'ID' |
|---|---|---|
| Behaves like | Double-quoted string | Single-quoted string |
| Variables interpolated | Yes | No |
Escape sequences (\n, \t, \$) | Processed | Not processed (not even |