PHP nl2br() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, nl2br() inserts an HTML line break tag before every newline in a string.

It helps show text from a textarea as separate lines in a web page.

Syntax

Call nl2br() with the values you need. Start with this line.

PHP
$html = nl2br("Line 1\nLine 2");
CallWhat it does
nl2br("A\nB")Returns "A<br />" plus a newline plus "B".
nl2br("A\nB", false)Returns "A<br>" plus a newline plus "B".
nl2br(htmlspecialchars($text))Escapes HTML first, then adds the line breaks.

The original newline characters stay in the string. The br tag is added before them.

Library declarations
PHP
nl2br(string $string, bool $use_xhtml = true): string

Parameters

ParameterDescription
stringRequired. The input string.
use_xhtmlOptional. If true, uses <br />. If false, uses <br>. Default is true.

Return Value

Return ValueDescription
stringThe string with a <br> tag before each newline.

PHP Examples

Example 1 — Add Line Breaks

Turn plain text new lines into HTML breaks.

PHP
<?php
$text = "Line 1\nLine 2";

echo nl2br($text), PHP_EOL;

How It Works

  1. Build a two-line string. The \n splits the lines.
  2. Call nl2br. A br tag is added before the newline.
  3. The newline stays. The text still wraps in the page source.
  4. Browsers read the tag. In a web page you would see two lines.

Example 2 — Use the Short br Tag

Pass false to get <br> instead of <br />, which is common in HTML5.

PHP
<?php
echo nl2br("A\nB", false), PHP_EOL;
echo nl2br("No newline here"), PHP_EOL;

How It Works

  1. Pass false. This is the second argument.
  2. Get an HTML5 tag. The output is <br>.
  3. No newline means no change. The second line is untouched.
  4. Windows newlines work too. \r\n counts as one break.

Example 3 — Safe Output for User Text

Escape HTML first, then add the breaks.

PHP
<?php
$text = "Hi <b>there</b>\nNew line";

echo nl2br(htmlspecialchars($text)), PHP_EOL;

How It Works

  1. Start with risky text. It has an HTML tag in it.
  2. Escape first. htmlspecialchars turns the tag into plain text.
  3. Add the breaks. nl2br works on the escaped string.
  4. Order matters. If you reverse the calls, the br tag would be escaped.

Frequently Asked Questions

nl2br() inserts an HTML <br /> tag before every newline character in a string so the line breaks show up in a web page.
string nl2br(string $string, bool $use_xhtml = true). Pass false to get <br> instead of <br />.
No. The br tag is added before them and the original newlines stay in the string.
Not by itself. It does not escape HTML. Use htmlspecialchars() first, then nl2br().
It handles \n, \r\n, \r, and \n\r, so text from any operating system works.

Next: number_format()

number_format() formats a number with grouped thousands and a chosen number of decimals.

number_format() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful