HOWTO · PHP
How to Write Multi-Line Strings in PHP
Learn to write multiline PHP strings with heredoc, nowdoc, newline escapes, and PHP_EOL.
On this page
To write a multiline string in PHP, use heredoc or nowdoc for literal text that spans lines, add \n or \r\n inside a double-quoted string, or append fragments with .= and PHP_EOL. These approaches put line-break characters in the string; they do not control visual wrapping, which depends on where the text is displayed. Choose the form that makes interpolation and the required line-ending convention clear.
Use heredoc or nowdoc for multiline string literals
Heredoc and nowdoc let you format a string across several source lines without writing an escape sequence at each break. Heredoc behaves like a double-quoted string, so PHP interpolates variables. Nowdoc behaves like a single-quoted string, so variable names remain literal. The closing identifier must match the opening identifier; it is itself the closing delimiter and must follow PHP’s identifier rules.
Run this example:
<?php
$name = 'Mina';
$message = <<<TEXT
Hello, $name.
Your report is ready.
TEXT;
$template = <<<'TEXT'
Hello, $name.
Your report is ready.
TEXT;
echo $message, PHP_EOL, $template;
The heredoc value contains Mina where $name appears. In the nowdoc value, $name stays part of the text. Both values contain a line break between their two source lines. Use heredoc for readable templates with variable values and nowdoc when the contents should stay literal.
Add line breaks with escape sequences
In a double-quoted string, \n adds a line-feed character. Use it when you want to keep a short string on one source line or control exactly where its lines break. Two \n escapes in succession leave a blank line between text. A single-quoted string does not interpret \n as a line feed, so use double quotes for this form.
The sequence \r\n contains a carriage return followed by a line feed. Together these characters are commonly treated as one line break by text viewers. A carriage return does not mean that PHP moves a text pointer to the left; it is a character in the string. Choose this sequence when the receiving format specifically calls for CRLF, such as a Windows-style text file. For ordinary platform-specific line separation, use PHP_EOL as described below.
Build multiline strings with concatenation and PHP_EOL
The .= operator appends its right-hand value to the string on its left. Start with the first fragment, then append each later fragment along with a separator. PHP_EOL supplies the line-ending sequence conventionally used by the platform running PHP.
<?php
$message = 'First line';
$message .= PHP_EOL . 'Second line';
$message .= PHP_EOL . 'Third line';
echo $message;
This sequential form keeps each addition easy to read: after the first assignment, $message holds the accumulated text, and every .= extends it. Add PHP_EOL before a fragment when that fragment should start on a new line. The final fragment does not need a trailing separator unless the consumer expects one.
Choose a newline format for your use case
Use PHP_EOL when the string will be written as ordinary text for the current platform and its conventional line ending is appropriate. It is a platform constant, not a guarantee that every receiving system will accept the same bytes. If a protocol, file format, or external service specifies LF or CRLF, include that explicit sequence instead of relying on the host platform.
Heredoc and nowdoc are mainly choices about how to express multiline literal content and whether variables are interpolated; the literal line breaks in their source become part of the string. Escape sequences are useful for short strings or when a precise separator matters. Concatenation is useful when pieces are assembled from variables or computed values. In each case, check the requirements of the destination: a terminal, browser, text file, and protocol may display or interpret line endings differently.