What are the potential security risks of not properly escaping special characters in PHP output?

Not properly escaping special characters in PHP output can lead to security vulnerabilities such as cross-site scripting (XSS) attacks, where an attacker can inject malicious scripts into a web page viewed by other users. To prevent this, you should always sanitize and escape user input before displaying it on a web page.

$user_input = "<script>alert('XSS attack!');</script>";
echo htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');