What are the potential pitfalls of using htmlentities or htmlspecialchars in PHP?

Using htmlentities or htmlspecialchars in PHP can potentially lead to double-encoding issues if the input data is already encoded. To prevent this, you can use the flag `ENT_QUOTES` in the htmlentities function to encode double quotes as well.

$input = '<script>alert("XSS attack")</script>';
$safe_input = htmlentities($input, ENT_QUOTES);
echo $safe_input;