← Back to PHP Course | Chapter 6: Strings | Lesson 6 of 8

PHP heredoc & nowdoc

Heredoc and nowdoc let you write long, many-line text without fussing over quotes, like writing a whole paragraph on a big page. Heredoc fills in variables, while nowdoc keeps everything exactly as written.
Syntax
php
$heredoc = <<<EOT
Text with $variable interpolated
EOT;

$nowdoc = <<<'EOT'
Text taken literally, no $variable replacement
EOT;

Understanding Heredoc

Heredoc syntax (<<<EOT ... EOT) lets you write a multi-line string without escaping every quote, while still interpolating variables inside it just like a double-quoted string would.

It's handy for embedding a block of HTML or a long message directly in PHP code.

Example: Understanding Heredoc

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
$text = <<<EOT
Hello, $name!
Welcome to PHP.
EOT;
// Print `$text` to the output
echo $text;
?>

Understanding Nowdoc

Nowdoc syntax (<<<EOT ... EOT, note the single quotes around the identifier) works like heredoc but never interpolates variables, behaving exactly like a single-quoted string spread across multiple lines.

Reach for it when your multi-line text contains literal $ characters you don't want PHP to try to expand.

Example: Understanding Nowdoc

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
$text = <<<'EOT'
Hello, $name!
This is literal, not interpolated.
EOT;
// Print `$text` to the output
echo $text;
?>

Multiline Block Generation

The closing identifier must start at the beginning of its own line in older PHP versions, though PHP 7.3+ relaxed this to allow indentation matching the opening marker, making heredoc/nowdoc blocks easier to keep neatly indented inside functions.

Example: Multiline Block Generation

php
<?php
// Define the function `greet` with no parameters
function greet() {
    $msg = <<<EOT
    Hello!
    This is indented heredoc (PHP 7.3+).
    EOT;
    // Return `$msg` from this function
    return $msg;
}
// Print `greet()` to the output
echo greet();
?>

Escaping Rules inside Heredoc

Because heredoc supports interpolation, you can embed variables and even simple expressions like {$array[key]} directly inside the block, which keeps templated text readable compared to concatenating dozens of string pieces with dots.

Example: Escaping Rules inside Heredoc

php
<?php
// Declare `$data` as an array: `['key' => 'Alice']`
$data = ['key' => 'Alice'];
$text = <<<EOT
Name: {$data['key']}
EOT;
// Print `$text` to the output
echo $text;
?>

Flexible Indentation

Heredoc and nowdoc are commonly used for embedding SQL queries, JSON payloads, or HTML fragments where the text is long and full of quote characters that would otherwise need constant escaping in a normal quoted string.

Example: Flexible Indentation

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
$json = <<<EOT
{"name": "$name", "active": true}
EOT;
// Print `$json` to the output
echo $json;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Indenting the closing heredoc identifier in PHP older than 7.3, which causes a parse error because it had to start at the beginning of the line.
  2. Expecting variables to be replaced inside a nowdoc, when a nowdoc (<<<EOT) prints the text literally and only heredoc interpolates.
  3. Adding a semicolon or other text after the closing identifier in old PHP versions, which breaks the heredoc.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.