PHP Reference ยท Language Constructs and Statements
PHP heredoc and nowdoc syntax
Reference for PHP heredoc and nowdoc: write multi-line strings with or without interpolation, closing marker indentation, escapes and common mistakes.
Heredoc and nowdoc are two ways to write long, multi-line strings without escaping quotes. A heredoc behaves like a double-quoted string: variables and escape sequences are processed. A nowdoc behaves like a single-quoted string: everything is literal.
Both start with <<< and a label, and end with the same label on a line of its own. Since PHP 7.3 the closing label may be indented, and that indentation is removed from every line of the string.
$name = "Ada"; echo <<<TEXT Hello, $name! TEXT; echo <<<'RAW' No $interpolation here\n RAW;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
<<<LABEL text with $variables and \escapes LABEL; <<<"LABEL" // same as heredoc, label quoted <<<'LABEL' // nowdoc: no interpolation or escapes
| Part | Description |
|---|---|
| Opening line | <<< followed by the label (optionally in " or ' quotes), then a newline, with nothing else on the line. |
| Body | The text. Heredoc interpolates $var, {$expr}, ${name} and processes escapes such as \n, \t, \\, \$. Nowdoc keeps every character as is. |
| Closing label | The label alone at the start of a line, or indented by spaces or tabs (not both). Text may follow on the same line since 7.3, for example ; or ). |
Result: a string. The newline right before the closing label is not part of it.
Errors: an inconsistent indentation, an indentation mixing tabs and spaces, or a body line indented less than the closing label is a parse error ("Invalid body indentation level").
Usage notes
- Choosing between them. Use heredoc for templates with a few variables, nowdoc for literal text such as code, regular expressions with many
$and backslashes, or SQL that must not be interpolated. - Indentation removal. The closing label's indentation is stripped from every body line, so the string can be indented to match your code. Lines may be indented more; the extra stays.
- Complex expressions need braces.
{$user['name']}and{$obj->method()}work. Unbraced$user[name]is allowed only in the simple form for plain keys; braces are clearer. - Calling functions.
{$fn(21)}works for variables holding closures. Plain function names cannot be called inside a string, so compute the value first. - Escaping the label. If the label appears inside the text on its own at the start of a line, the heredoc ends early. Pick a distinctive label such as
HTML,SQLorEOT. - Not for user input in SQL or HTML. Interpolating raw values into SQL is an injection risk; use prepared statements. Escape output for HTML with
htmlspecialchars(). - Allowed in constants and default values. A nowdoc, and a heredoc without variables, can be used in
const, property defaults and parameter defaults, since they are constant expressions. - Line endings follow your file: a file saved with
\r\nyields\r\nin the string.
Related
string,echo,sprintf(),htmlspecialchars()- Lessons: Strings, String formatting