What is the purpose of nl2br function in PHP and how is it commonly used?

The nl2br function in PHP is used to insert HTML line breaks (<br>) before all newlines in a string. This is commonly used when displaying text from a database or form input in an HTML document, as HTML does not recognize newline characters (\n) on its own.

// Example usage of nl2br function
$text = &quot;Hello\nWorld!&quot;;
echo nl2br($text);
```

This code snippet will output:
```
Hello&lt;br&gt;World!