What are the common pitfalls when trying to display alert boxes using PHP and how can they be avoided?

Common pitfalls when trying to display alert boxes using PHP include not properly escaping user input, not using the correct syntax for alert boxes, and not including the necessary HTML tags to display the alert box. To avoid these pitfalls, make sure to sanitize user input using functions like htmlspecialchars(), use the correct JavaScript syntax for alert boxes, and include the necessary HTML tags for displaying the alert box.

<?php
// Sanitize user input
$message = htmlspecialchars($_GET['message']);

// Display alert box using JavaScript
echo "<script>alert('$message');</script>";
?>