What are the best practices for handling user input that includes line breaks in PHP?

When handling user input that includes line breaks in PHP, it's important to sanitize the input to prevent any potential security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to handle this is by using the nl2br() function in PHP, which converts newlines into HTML line breaks. This function allows the user input to be displayed on a webpage with proper formatting while still preventing any malicious code execution.

$user_input = $_POST['user_input']; // Assuming user input is submitted via a form
$sanitized_input = nl2br(htmlspecialchars($user_input));

echo $sanitized_input; // Display the sanitized user input on the webpage