I’m writing a small extenstion for PHP. Is there way to know at runtime, the name of the script file (e.g.: test.php) that is running? Maybe some global or environment variables?
I’m writing a small extenstion for PHP. Is there way to know at runtime,
Share
You can fetch
$_SERVER['PHP_SELF'](or any other $_SERVER variable if you need to), like this:The
script_namevariable will contain the name of the script.In case you’re wondering, the first block, that initializes
$_SERVER, is necessary because some SAPIs (e.g.: the Apache handler) will initialize$_SERVERonly when the user script accesses it (just-in-time). Without that block of code, if you try to read$_SERVER['PHP_SELF']before the script tried accessing$_SERVER, you’d end up with an empty value.Obviously, you should add error handling in the above code in case anything fails, so that you don’t invoke undefined behavior when trying to access
script_name.