Computer >> Computer tutorials >  >> Programming >> PHP

nl2br() function in PHP


The nl2br() function inserts HTML line breaks before all newlines in a string The new lines are generally denoted by the following −

  • \n\r
  • \r\n
  • \n
  • \r

Syntax

nl2br(str, xhtml)

Parameters

  • str  − The string to check

  • xhtml  − A boolean value indicating whether or not to use XHTML compatible line breaks

    • TRUE − Inserts
      . This is the default.

    • FALSE − Inserts

Return

The nl2br() function returns the converted string.

Example

The following is an example −

<?php
   echo nl2br("This has multiple \nlines\nhere!",false);
?>

Output

This has multiple
lines
here!

Example

The following is an example −

<?php
   echo nl2br("This is\r\ndemo text!", false);
?>

Output

This is
demo text!