What are the best practices for handling line breaks and formatting in PHP scripts to avoid unexpected characters in the output?
When handling line breaks and formatting in PHP scripts, it's important to use the correct escape characters to avoid unexpected characters in the output. One common issue is when line breaks are not properly handled, leading to unwanted characters such as newlines or carriage returns in the output. To solve this problem, you can use the PHP function `nl2br()` to convert newlines to HTML line breaks, ensuring proper formatting in the output.
// Example of handling line breaks and formatting using nl2br()
$text = "Hello\nWorld!";
$formatted_text = nl2br($text);
echo $formatted_text;