What function in PHP can be used to convert newline characters to HTML line breaks?

To convert newline characters to HTML line breaks in PHP, you can use the nl2br() function. This function takes a string as input and replaces newline characters (\n) with <br> tags, which are recognized as line breaks in HTML. This is useful when displaying text that contains newline characters in an HTML document.

$text_with_newlines = &quot;This is a text\nwith\nnewlines.&quot;;
$text_with_linebreaks = nl2br($text_with_newlines);

echo $text_with_linebreaks;