What are some best practices for handling special characters like new lines (\n) in PHP output?

Special characters like new lines (\n) can cause formatting issues when outputting text in PHP. To handle these special characters properly, you can use the nl2br() function in PHP, which converts new lines (\n) to HTML line breaks (<br>). This ensures that the text is displayed correctly with proper line breaks in HTML output.

$text = &quot;This is a text with\na new line.&quot;;
echo nl2br($text);