What are the potential pitfalls of using htmlspecialchars incorrectly in PHP code?

Using htmlspecialchars incorrectly in PHP code can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To prevent this, always use htmlspecialchars to escape user input before displaying it on a webpage to ensure that any special characters are properly encoded. This will help prevent malicious scripts from being executed by the browser.

// Incorrect usage of htmlspecialchars
$unsafe_input = "<script>alert('XSS attack!');</script>";
echo htmlspecialchars($unsafe_input);
```

```php
// Correct usage of htmlspecialchars to prevent XSS attacks
$unsafe_input = "<script>alert('XSS attack!');</script>";
echo htmlspecialchars($unsafe_input, ENT_QUOTES, 'UTF-8');