How can PHP developers ensure that line breaks are preserved when passing form data between different pages?

When passing form data between different pages in PHP, developers can ensure that line breaks are preserved by using the nl2br() function to convert newline characters to HTML line breaks. This function will maintain the formatting of the text entered in a form field, displaying it with line breaks when the data is displayed on another page.

// Using nl2br() function to preserve line breaks in form data
$form_data = $_POST['form_data']; // Assuming form data is submitted via POST method
$form_data_with_line_breaks = nl2br($form_data);

// Displaying the form data with preserved line breaks
echo $form_data_with_line_breaks;