In what scenarios is it advisable to use $_SERVER['PHP_SELF'] instead of PHP_SELF when dealing with form submissions and variable passing in PHP scripts?

When dealing with form submissions and variable passing in PHP scripts, it is advisable to use $_SERVER['PHP_SELF'] instead of PHP_SELF to prevent potential security vulnerabilities such as Cross-Site Scripting (XSS) attacks. By using $_SERVER['PHP_SELF'], you ensure that the form submission points to the current script, reducing the risk of malicious code injection.

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