HOWTO · PHP
How to Create a New Line in PHP
Choose the right PHP line break for text strings, platform-native files, or HTML output. See how ` `, PHP_EOL, <br>, and CRLF behave.
On this page
To create a line break in PHP, match the representation to where the output will be read. Use \n inside a double-quoted string for a line break in text, and PHP_EOL when text should use the current platform’s native line-ending sequence. For a visible break in a browser, emit HTML such as <br>; a newline in the HTML source alone usually does not create a rendered line break. These choices are not interchangeable: a browser interprets HTML, while a terminal or text-file reader consumes characters.
Choose the newline for the output format
For CLI messages, logs, and plain-text files, put a newline character in the string or use PHP_EOL when the file should follow the platform convention. For HTML responses, use markup that the browser understands. Decide based on the output consumer, not simply on the operating system running PHP. If output might be read both as text and as HTML, create separate formats for those consumers rather than mixing markup and text line endings in one string.
Use \n in a PHP string
In a double-quoted PHP string, \n represents a line feed. A single-quoted string does not interpret \n as a line break; it preserves the backslash sequence (with PHP’s limited single-quote escape rules). Use double quotes when you want PHP to interpret this escape. Two consecutive line feeds leave a blank line between text lines.
Run this example:
<?php
$message = "Hello, PHP!\nWelcome back.";
echo $message, "\n";
?>
Expected output:
Hello, PHP!
Welcome back.
This is suitable for terminal output and text content. When writing a file, the bytes in the string are written as-is, so choose the line-ending convention expected by the program that will read that file.
Use PHP_EOL for platform-native text
PHP_EOL contains the line-ending sequence for the platform where PHP runs. Concatenate it between text lines, for example, "First" . PHP_EOL . "Second". It is useful for CLI output and text files intended for that environment. It does not create a visual HTML break: a browser may collapse source whitespace when rendering a paragraph. Also, a file intended for a specific interchange format may require a fixed line ending, so use that format’s required characters instead of assuming the host platform is the right choice.
Use <br> for HTML line breaks
When generating HTML, <br> is an HTML line-break element. For example, PHP can output "First line<br>Second line", and an HTML renderer displays the text on separate lines. A newline character in HTML source is generally treated as whitespace, not as a guaranteed visible break. Conversely, <br> printed in a terminal appears as literal markup and does not move the cursor to a new line. Escape untrusted values before inserting them into HTML; the line-break element does not make surrounding content safe.
Understand \r\n and blank lines
\r\n consists of a carriage return followed by a line feed. It is the conventional line ending in many Windows text files. In a PHP double-quoted string, the two escapes produce those two control characters. Use this sequence when a receiving format explicitly expects CRLF; otherwise, PHP_EOL is convenient for platform-native text and \n is common in formats that specify line feed. A blank line is made by adding another complete line-ending sequence, such as "First\n\nSecond". Carriage return is not a PHP instruction to reset an output pointer; it is a character whose display effect depends on the output device.
Common mistakes and quick checks
- If
\nappears literally, check whether the string uses single quotes. Use double quotes or construct the line break another way. - If a browser shows text on one visual line, check the rendered HTML. Use
<br>or suitable CSS rather than relying on whitespace in the source. - If
<br>appears in a terminal or saved plain-text file, the output is being consumed as text. Use\norPHP_EOLinstead. - If another program rejects a text file, check its required line-ending convention.
\n,\r\n, andPHP_EOLcan produce different byte sequences.
Choose the representation for the next reader of the output: PHP string syntax for text line feeds, PHP_EOL for native text files, the specified sequence for a fixed file format, and <br> for browser-rendered HTML.