PHP Line Breaks: \n, PHP_EOL and nl2br() Explained

Adding a line break in PHP looks simple until you display the output somewhere else.

A new line that works perfectly in a text file may appear as a single long paragraph in a browser. This is one of those small PHP details that developers usually discover while debugging output at 11 PM.

PHP provides different ways to create and handle line breaks depending on where the output is going. In this article, we will see how to add line breaks using \n, PHP_EOL, and how to convert text line breaks into HTML line breaks using nl2br().

Quick Answer

Use \n or PHP_EOL when creating new lines in PHP strings. If you need to display a PHP string with line breaks inside an HTML page, use nl2br().

<?php
$message = "First line\nSecond line\nThird line";

echo nl2br($message);
?>

The output will display each sentence on a separate line in the browser.

Adding Line Breaks in PHP Strings

PHP supports escape sequences inside double-quoted strings. The most commonly used line break character is \n.

<?php
echo "Hello\nWorld";
?>

The above code creates a new line between Hello and World.

The \n character represents a newline character. It is commonly used when generating text output, log files, email content, and plain text files.

Using PHP_EOL for Platform-Friendly Line Breaks

PHP also provides the PHP_EOL constant. It returns the correct end-of-line character for the operating system where PHP is running.

<?php
echo "First line" . PHP_EOL;
echo "Second line";
?>

For example:

  • Windows uses \r\n
  • Linux uses \n
  • macOS uses \n

Using PHP_EOL is useful when creating files that should follow the operating system’s native line ending format.

The official PHP documentation lists PHP predefined constants, including constants available for different PHP environments.

For browser output, however, PHP_EOL alone will not create a visible line break. Browsers ignore plain newline characters in HTML output.

Why PHP Line Breaks Do Not Work in HTML Output

A common confusion with PHP line breaks happens when developers echo text directly into a web page.

Consider this example:

<?php
echo "Line one\nLine two\nLine three";
?>

The PHP code contains newline characters, but the browser will usually display the output like this:

Line one Line two Line three

The reason is that HTML does not treat whitespace and newline characters as visible line breaks. The browser receives the text, but HTML rendering rules collapse those spaces and line endings.

To display line breaks in HTML, you need to convert newline characters into HTML line break tags using the nl2br() function.

Using nl2br() to Display PHP Line Breaks in HTML

The PHP nl2br() function inserts HTML <br> tags before all newline characters in a string.

You can find the complete function details and examples in the official PHP documentation for nl2br().

Example:

<?php
$text = "PHP is simple.\nPHP is powerful.\nPHP is widely used.";

echo nl2br($text);
?>

The browser receives the converted HTML output:

PHP is simple.<br>
PHP is powerful.<br>
PHP is widely used.

The displayed result will appear as:

PHP is simple.
PHP is powerful.
PHP is widely used.

This is especially useful when displaying user-entered text, comments, messages, or database content where line breaks need to be preserved.

nl2br() Syntax

nl2br(string $string, bool $use_xhtml = true): string

The first parameter is the string that contains newline characters. The second optional parameter controls whether the function generates XHTML-compatible line breaks.

<?php
$message = "First line\nSecond line";

echo nl2br($message, false);
?>

When the second parameter is set to false, PHP generates:

First line<br>
Second line

By default, nl2br() generates XHTML-style <br /> tags for backward compatibility.

Modern HTML5 pages accept both forms, so the default behaviour is usually fine for most applications.

PHP Line Breaks in Different Output Scenarios

The correct way to add a line break in PHP depends on where the output will be used. The same PHP string may need different handling when displayed in a browser, written to a file, or sent through email.

1. Line Breaks in Browser Output

For HTML pages, use <br> tags or convert existing newline characters using nl2br().

<?php
echo "First paragraph.<br>";
echo "Second paragraph.";
?>

For dynamic content, nl2br() is usually cleaner:

<?php
$description = "Fast PHP development.\nClean and maintainable code.";

echo nl2br($description);
?>

2. Line Breaks in Text Files

When writing text files, use PHP_EOL if you want the generated file to follow the server operating system’s line-ending convention.

<?php
$content = "First record" . PHP_EOL;
$content .= "Second record";

file_put_contents("sample.txt", $content);
?>

This creates a text file with separate lines:

First record
Second record

3. Line Breaks in Email Content

When sending plain-text emails, newline characters are normally used directly.

<?php
$emailBody = "Hello John,\n\nYour account has been created.\n\nThank you.";
?>

For HTML emails, use HTML formatting instead:

<p>Hello John,</p>

<p>Your account has been created.</p>

<p>Thank you.</p>

Difference Between \n, PHP_EOL, and <br>

Method Purpose Common Usage
\n Adds a newline character inside a string Text output, logs, email messages
PHP_EOL Adds an operating system-specific line ending Creating files and command-line output
<br> Creates a visible line break in HTML Browser output
nl2br() Converts newline characters into HTML line breaks Displaying stored text in web pages

A simple rule to remember:

  • Creating text? Use \n.
  • Creating files? Prefer PHP_EOL.
  • Displaying stored text in HTML? Use nl2br().

Handling User Input with PHP Line Breaks Safely

A common use case for nl2br() is displaying text entered by users, such as comments, feedback, or profile descriptions.

However, nl2br() only converts newline characters. It does not make user input safe for HTML output.

For example, this code is not safe:

<?php
$comment = $_POST['comment'];

echo nl2br($comment);
?>

If the input contains HTML or JavaScript code, it will be interpreted by the browser.

Escape the output before displaying it:

<?php
$comment = $_POST['comment'];

echo nl2br(htmlspecialchars($comment, ENT_QUOTES, 'UTF-8'));
?>

The flow is:

  1. htmlspecialchars() converts special HTML characters into safe entities.
  2. nl2br() adds HTML line break tags.
  3. The browser displays the text while preserving the original line formatting.

This pattern is useful whenever plain text from users needs to be displayed inside an HTML page.

Common PHP Line Break Problems and Fixes

Problem 1: \n Does Not Create a New Line in Browser

Code:

<?php
echo "Hello\nWorld";
?>

The output appears on one line because HTML ignores plain newline characters.

Fix:

<?php
echo nl2br("Hello\nWorld");
?>

Problem 2: Using Single Quotes with \n

Escape sequences such as \n are interpreted only inside double-quoted strings.

This will not create a newline:

<?php
echo 'First line\nSecond line';
?>

Output:

First line\nSecond line

Use double quotes instead:

<?php
echo "First line\nSecond line";
?>

Problem 3: Expecting PHP_EOL to Work in HTML

This code:

<?php
echo "Line one" . PHP_EOL . "Line two";
?>

creates a newline character, but browsers still display it as normal whitespace.

For HTML output, convert it:

<?php
echo nl2br("Line one" . PHP_EOL . "Line two");
?>

Developer FAQ

What is the difference between \n and PHP_EOL in PHP?

\n is a newline escape sequence used inside PHP strings. PHP_EOL is a PHP constant that returns the correct line-ending character for the current operating system.

Use \n for general string formatting. Use PHP_EOL when generating files or command-line output where operating system compatibility matters.

Why is my PHP newline not working?

If the output is displayed in a browser, newline characters like \n are ignored by HTML rendering. Use nl2br() to convert newline characters into HTML line breaks.

Can I use <br> directly in PHP?

Yes. Since PHP can output HTML, you can directly print <br>.

<?php
echo "First line<br>";
echo "Second line";
?>

However, when working with dynamic text that already contains newline characters, nl2br() is usually a better approach.

Does nl2br() add actual newline characters?

No. The nl2br() function does not change the original newline characters. It returns a new string with HTML <br> tags inserted before newline characters.

Should I use nl2br() before or after htmlspecialchars()?

When displaying user-provided plain text in HTML, use htmlspecialchars() first and then apply nl2br().

<?php
$text = "Hello\nWelcome";

echo nl2br(htmlspecialchars($text, ENT_QUOTES, 'UTF-8'));
?>

This keeps the content safe while preserving line breaks.

Conclusion

PHP line breaks are simple once you know where the output is going.

Use \n when you need a newline inside a PHP string, PHP_EOL when creating platform-friendly text files, and nl2br() when displaying stored text inside an HTML page.

The important thing to remember is that PHP creates the line break, but the output environment decides whether that line break is visible.

Photo of Vincy, PHP developer
Written by Vincy Last updated: July 22, 2026
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Continue Learning

These related tutorials may help you continue learning.

6 Comments on "PHP Line Breaks: \n, PHP_EOL and nl2br() Explained"

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?