How can SCRIPT_NAME in $_SERVER be utilized to address the issue of extracting only the file name from a URL in PHP?

When extracting only the file name from a URL in PHP, the SCRIPT_NAME variable in the $_SERVER superglobal can be utilized. This variable contains the file name of the currently executing script, making it a convenient way to extract just the file name from a URL.

$url = 'http://www.example.com/path/to/file.php';
$file_name = basename($_SERVER['SCRIPT_NAME']);
echo $file_name; // Outputs 'file.php'