0

Heredoc and Nowdoc string syntax

Beginner5 min read·php-02-017

Concept

Heredoc and Nowdoc are PHP's multi-line string syntaxes — alternatives to single and double quotes when you need strings that span many lines without escaping every quote.

Heredoc (<<<LABEL) behaves like a double-quoted string: variables are interpolated and escape sequences like \n and \t are processed. The opening <<<LABEL must be on its own line. The closing LABEL; must be at the start of a line (no leading whitespace in PHP < 7.3; indented closing allowed since PHP 7.3).

Nowdoc (<<<'LABEL') behaves like a single-quoted string: no interpolation, no escape sequences, content is completely literal. Use it for SQL, HTML templates, or code snippets where you want zero processing.

Since PHP 7.3, both allow the closing marker to be indented, and leading whitespace equal to the indent of the closing marker is stripped from every line — making heredoc/nowdoc usable inside indented class methods without ugly formatting.

Code Example

php
<?php
declare(strict_types=1);

$name = 'Codrut';
$version = '8.4';

// Heredoc — interpolates variables (like double-quoted string)
$html = <<<HTML
    <div class="greeting">
        <h1>Hello, {$name}!</h1>
        <p>Running PHP {$version}</p>
    </div>
    HTML;
// The 4 spaces of indentation on closing HTML are stripped from all lines

// Nowdoc — zero interpolation (like single-quoted string)
$sql = <<<'SQL'
    SELECT u.id, u.name
    FROM users u
    WHERE u.name = '$name'  -- literal $name, NOT interpolated
    SQL;

// Heredoc as function argument (PHP 5.3+)
echo sprintf(<<<'TPL'
    Name: %s
    Version: %s
    TPL, $name, $version);

// Heredoc for complex HTML generation
function renderCard(string $title, string $body): string
{
    return <<<HTML
        <article class="card">
            <h2>{$title}</h2>
            <p>{$body}</p>
        </article>
        HTML;
}

Interview Q&A

Q: What is the difference between heredoc and nowdoc in PHP?

Heredoc (<<<LABEL) works like a double-quoted string — variables are interpolated and escape sequences (\n, \t, etc.) are processed. Nowdoc (<<<'LABEL') works like a single-quoted string — the content is completely literal with no processing at all. Use heredoc when you need variable substitution in multi-line strings; use nowdoc for SQL queries, code templates, or any string where $ signs must appear literally.


Q: What changed in PHP 7.3 regarding heredoc/nowdoc indentation?

Before PHP 7.3, the closing marker had to be at column zero (no leading whitespace), which forced ugly formatting when using heredoc inside an indented method. PHP 7.3 introduced flexible heredoc/nowdoc: the closing marker can be indented, and PHP strips that exact amount of leading whitespace from every line of the string. This makes heredoc usable inside class methods without breaking code indentation — the string content is still correctly indented relative to its context.


Q: When should you prefer heredoc/nowdoc over string concatenation or sprintf?

Use heredoc/nowdoc for multi-line strings with many embedded variables or HTML/SQL structure where concatenation would produce unreadable code. Use sprintf when you have a short, single-line format string with a few placeholders — it's more readable for simple substitutions. Avoid heredoc inside tight loops where the parser overhead of a large string matters; pre-define it as a constant or class property instead. For template-heavy code, a proper template engine (Blade, Twig) is usually better than either approach.