What are the best practices for maintaining line breaks in textareas in PHP forms?

When submitting text with line breaks in a textarea form field, PHP may not preserve those line breaks when displaying the submitted text. To maintain line breaks in textareas in PHP forms, you can use the nl2br() function to convert newline characters to HTML line breaks (<br>). This will ensure that the line breaks are displayed correctly when the submitted text is shown on a webpage.

// Retrieve the text from the textarea form field
$text = $_POST[&#039;textarea_field&#039;];

// Convert newline characters to HTML line breaks
$text_with_line_breaks = nl2br($text);

// Display the text with preserved line breaks
echo $text_with_line_breaks;