What is the correct way to access the PHP_SELF variable in PHP forms?

When accessing the PHP_SELF variable in PHP forms, it is important to sanitize the input to prevent potential security vulnerabilities such as cross-site scripting (XSS) attacks. One way to do this is by using the htmlspecialchars() function to escape special characters in the input.

<?php
$self = htmlspecialchars($_SERVER['PHP_SELF']);
?>