What are the best practices for handling line breaks in text fields when working with PHP?

When working with text fields in PHP, it is important to handle line breaks properly to ensure the formatting is maintained. One common issue is that line breaks may be displayed differently depending on the platform or browser being used. To solve this, you can use the PHP function nl2br() to convert newline characters to HTML line breaks <br> before displaying the text on a webpage.

$text = &quot;This is a text with\nline breaks.&quot;;
$formatted_text = nl2br($text);
echo $formatted_text;