What is the function of nl2br in PHP and when should it be used?
The nl2br function in PHP is used to insert HTML line breaks (<br>) before all newlines in a string. This is useful when displaying text that contains line breaks in an HTML document, as HTML ignores single newlines.
$text = "Hello\nWorld!";
echo nl2br($text);
```
This code snippet will output:
```
Hello
World!