How does the nl2br() function in PHP differ from using str_replace() for line breaks?
The nl2br() function in PHP is specifically designed to convert newline characters (\n) into HTML line breaks (<br>). This function is more efficient and convenient for handling line breaks compared to using str_replace() because it specifically targets newline characters for conversion. Using nl2br() ensures consistent and correct formatting of line breaks in HTML output.
// Example of using nl2br() function to convert newline characters to HTML line breaks
$text = "Hello\nWorld!";
echo nl2br($text);