The nl2br Function

The nl2br function inserts a br tag before the newline character. The newline character is a special invisible character that denotes places where the Enter key was pressed. This character is usually not visible in text editors, but it exists - it is \n.

Syntax

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

Example

In this example, the function will insert a br tag before the newline:

php<?php
  echo nl2br('hello
  world');
?>

Code execution result:

php'hello<br>
world'

See Also

  • the wordwrap function,
    which inserts a line break character