How can the nl2br function be effectively used in PHP to handle line breaks in user-submitted text?

When users submit text with line breaks in a textarea, those line breaks are represented by "\n" characters in the PHP code. To properly display these line breaks in HTML, the nl2br function can be used to convert "\n" characters to "<br>" tags. This allows the user-submitted text to be displayed with the appropriate line breaks in the browser.

&lt;?php
$user_text = $_POST[&#039;user_text&#039;]; // Get user-submitted text from a form

// Use nl2br function to convert &quot;\n&quot; characters to &quot;&lt;br&gt;&quot; tags
$formatted_text = nl2br($user_text);

// Display the formatted text with line breaks in HTML
echo $formatted_text;
?&gt;