What are the potential pitfalls of using PHP_SELF in a script and why should it be avoided?

Using PHP_SELF in a script can expose your application to potential security vulnerabilities such as Cross-Site Scripting (XSS) attacks. It is recommended to avoid using PHP_SELF and instead use htmlentities() or htmlspecialchars() to sanitize user input before displaying it back to the user.

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <!-- form fields -->
</form>