``` "> ``` " />
Easy Tutorial
❮ Func Curl_Multi_Setopt Php Settype Function ❯

PHP nl2br() Function

PHP String Reference Manual

Example

Inserting a line break before new lines (\n) in a string:

<?php
echo nl2br("One line.\nAnother line.");
?>

The browser output of the above code is as follows:

One line.
Another line.

The HTML input of the above code is as follows (view source):

One line.<br />
Another line.

Definition and Usage

The nl2br() function inserts HTML line breaks (<br> or <br />) before each new line (\n) in a string.


Syntax

Parameter Description
string Required. The string to be checked.
xhtml Optional. A boolean value indicating whether to use XHTML-compliant line breaks: TRUE - Default. Inserts <br />
FALSE - Inserts <br>

Technical Details

Return Value: Returns the converted string.
PHP Version: 4+
--- ---
Changelog: Before PHP 4.0.5, the function inserted <br>. After PHP 4.0.5, the function inserted XHTML-compliant <br />.
In PHP 5.3, the xhtml parameter was added.
--- ---

More Examples

Example 1

Inserting a line break before new lines (\n) using the xhtml parameter:

<?php
echo nl2br("One line.\nAnother line.", false);
?>

The browser output of the above code is as follows:

One line.
Another line.

The HTML input of the above code is as follows (view source):

One line.<br>
Another line.

❮ Func Curl_Multi_Setopt Php Settype Function ❯