What are the best practices for displaying user input data in HTML form elements, such as textareas, to avoid errors and ensure proper rendering?

When displaying user input data in HTML form elements like textareas, it is important to properly escape the data to prevent cross-site scripting attacks and ensure that the data is displayed correctly. One way to do this is by using the htmlspecialchars function in PHP to convert special characters to their HTML entities.

<textarea><?php echo htmlspecialchars($user_input_data); ?></textarea>