How can the PHP_SELF variable be used securely in PHP forms?

The PHP_SELF variable can be vulnerable to cross-site scripting (XSS) attacks if not properly sanitized. To use it securely in PHP forms, it is recommended to sanitize the variable using the htmlspecialchars() function to prevent any malicious scripts from being executed.

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