How can PHP be used to preserve line breaks in textareas when displaying the input?

When displaying input from a textarea in PHP, line breaks are often lost due to HTML not recognizing newline characters. To preserve line breaks, you can use the PHP nl2br() function, which converts newline characters (\n) into HTML line breaks (<br>). This allows the text to be displayed with line breaks intact.

&lt;?php
// Retrieve input from a textarea
$input = $_POST[&#039;textarea_input&#039;];

// Preserve line breaks using nl2br() function
$preserved_input = nl2br($input);

// Display the input with preserved line breaks
echo $preserved_input;
?&gt;