Skip to content
PHP Logo

How can the nl2br function in PHP be utilized to handle multi-line database values in HTML?

When retrieving multi-line database values in PHP and displaying them in HTML, the nl2br function can be used to convert newline characters (\n) to <br> tags. This allows the line breaks to be properly displayed in the browser.

php
<?php
// Retrieve the multi-line database value
$multiLineValue = "First line\nSecond line\nThird line";

// Use nl2br function to convert newline characters to <br> tags
$displayValue = nl2br($multiLineValue);

// Display the value in HTML
echo $displayValue;
?>