What are some common pitfalls when displaying user input in PHP and how can they be avoided?

One common pitfall when displaying user input in PHP is not properly sanitizing the input, which can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To avoid this, always use functions like htmlspecialchars() to escape special characters in user input before displaying it on a webpage.

$user_input = $_POST['user_input'];
$sanitized_input = htmlspecialchars($user_input);

echo $sanitized_input;