What function in PHP can be used to convert newline characters to HTML line breaks for displaying text from a database?

When displaying text from a database in HTML, newline characters (\n) need to be converted to HTML line breaks (<br>) in order to properly format the text with line breaks. This can be achieved using the PHP function nl2br(), which converts newline characters to <br> tags.

$text_from_database = &quot;This is a text with\nnewline characters.&quot;;
$formatted_text = nl2br($text_from_database);
echo $formatted_text;