How can PHP developers ensure that line breaks and formatting are preserved in user input while still preventing code execution?
To preserve line breaks and formatting in user input while preventing code execution, PHP developers can use the `htmlspecialchars()` function to escape special characters in the input. This function will convert characters like `<`, `>`, `&`, and `"` to their respective HTML entities, preventing them from being interpreted as code. By using this function, developers can ensure that the user input is displayed as intended without risking code execution.
$user_input = "<script>alert('Hello');</script>";
$escaped_input = htmlspecialchars($user_input, ENT_QUOTES);
echo $escaped_input;