What is the function nl2br() used for in PHP and when should it be applied?

The function nl2br() in PHP is used to convert newline characters (\n) into HTML line breaks (<br>). This is particularly useful when displaying text that contains line breaks in an HTML document, as HTML ignores newline characters.

```php
$text = &quot;This is a text with\na newline character.&quot;;
echo nl2br($text);
```

In this code snippet, the nl2br() function is applied to the $text variable before echoing it out. This will convert the \n into &lt;br&gt; tags, resulting in the text being displayed with proper line breaks in the HTML output.