PHP Heredoc

Summary: in this tutorial, you’ll learn how to use PHP heredoc and nowdoc strings to improve the readability of the code.

Introduction to the PHP heredoc string

When you place variables in a double-quoted string, PHP will expand the variable names. If a string contains the double quotes (“), you need to escape them using the backslash character(\). For example:

<?php

$he = 'Bob';
$she = 'Alice';

$text = "$he said, \"PHP is awesome\".
\"Of course.\" $she agreed.";

echo $text;Code language: PHP (php)

Output:

Bob said, "PHP is awesome".
"Of course." Alice agreed.Code language: PHP (php)

PHP heredoc strings behave like double-quoted strings, without the double-quotes. It means that they don’t need to escape quotes and expand variables. For example:

<?php

$he = 'Bob';
$she = 'Alice';

$text = <<<TEXT
$he said "PHP is awesome".
"Of course" $she agreed."
TEXT;

echo $text;Code language: PHP (php)

PHP heredoc syntax

The following shows the syntax of a heredoc string:

<?php

$str = <<<IDENTIFIER
place a string here
it can span multiple lines
and include single quote ' and double quotes "
IDENTIFIER;Code language: PHP (php)

How it works.

First, start with the <<< operator, an identifier, and a new line:

<<<IDENTIFIERCode language: PHP (php)

Second, specify the string, which can span multiple lines and includes single quotes (‘) or double quotes (“).

Third, close the string with the same identifier.

The identifier must contain only alphanumeric characters and underscores and start with an underscore or a non-digit character.

The closing identifier must follow these rules:

  • Begins at the first column of the line
  • Contains no other characters except a semicolon (;).
  • The character before and after the closing identifier must be a newline character defined by the local operating system.

The following shows an invalid heredoc string because the character before it is not a newline character:

<?php

$str = <<<IDENTIFIER
invalid
    IDENTIFIER;

echo $str;Code language: PHP (php)

However, the following heredoc string is valid:

<?php

$str = <<<IDENTIFIER
    valid
    IDENTIFIER;

echo $str;Code language: PHP (php)

PHP heredoc strings’ use cases

In practice, you use the heredoc syntax to define a string that contains a single quote, double quotes, or variables. The heredoc string makes the string easier to read.

Also, you can use a heredoc string to generate HTML dynamically. For example:

<?php

$title = 'My site';

$header = <<<HEADER
<header>
    <h1>$title</h1>
</header>
HEADER;

echo $header;Code language: PHP (php)

PHP Nowdoc syntax

A nowdoc string is similar to a heredoc string except that it doesn’t expand the variables. Here’s the syntax of a nowdoc string:

<?php

$str = <<<'IDENTIFIER'
place a string here
it can span multiple lines
and include single quote ' and double quotes "
IDENTIFIER;Code language: PHP (php)

The nowdoc’s syntax is similar to the heredoc’s syntax except that the identifier which follows the <<< operator needs to be enclosed in single quotes. The nowdoc’s identifier also follows the rules for the heredoc identifier.

Summary

  • Heredoc strings are like double-quoted strings without escaping.
  • Nowdoc strings are like single-quoted strings without escaping.
Did you find this tutorial useful?