What is the function nl2br in PHP and how can it be used to address the problem of preserving line breaks?

Preserving line breaks in PHP can be a challenge when displaying text that includes newline characters. The nl2br function in PHP is used to convert newline characters (\n) to HTML line breaks (<br>) so that the line breaks are preserved when rendering the text on a webpage.

$text = &quot;Hello\nWorld!&quot;;
echo nl2br($text);
```

In this code snippet, the variable $text contains the string &quot;Hello\nWorld!&quot;. When the nl2br function is applied to $text and echoed to the output, the newline character (\n) is converted to an HTML line break (&lt;br&gt;), resulting in the output:

```
Hello
World!