Is there any difference between basename(__FILE__); and $_SERVER['SCRIPT_NAME']?
Im just seeing people often use one of the 2, and Im unsure if it matters which one to use. I ofcourse tried them both, and they seem to output the same thing. Just wondering. :)
__FILE__ returns the filename of the script that is currently being run.
dirname returns the directory of a given filename
basename() returms the filename component of path. To put it better: it strips any "parent" directories off a given string.
$_SERVER['SCRIPT_NAME'] Returns the path of the current script
No. Both are same.They will return same thing But i think $_SERVER['SCRIPT_NAME'] is better as it is small and PHP global variable
Yes, there is a definitely a big difference you need take in consideration.
One is used to get the included script full path (current file) and the other is used to get the script path (relative to web server). So you might use one or the other depending of what you try to accomplish.
So it will return a different result in a different format:
__FILE__
: C:\xampp\htdocs\project\myfileincluded.php
$_SERVER['SCRIPT_NAME']
: /project/index.php
__FILE__
is part of the Magic constants and will return the absolute path of the current included file.
$_SERVER['SCRIPT_NAME']
is a part of the Predefined server variables, and will return the path of the script originally called in the server relative to the server root path.
You might want to use __FILE__
when logging messages or when looking to extract the current project name, branch name, etc.. And you might want to use $_SERVER['SCRIPT_NAME']
when you are looking to refresh the page, identify the origin of the call, and so on.
In your example you are using basename(__FILE__)
which will in that case return the name of the included file.
__FILE__
return the script name where this command is originally situated.$_SERVER['SCRIPT_NAME']
return the script name where this command is called.
For example, if you have "Script_A.php" that call a function "ThisFile()", where "ThisFile()" is in another "Script_B.php" through include_once, then the result will be different.
__FILE__
=> return Script_B$_SERVER['SCRIPT_NAME']
=> return Script A
Not so relevant but I think it's worth noticing. Be aware that there is also $_SERVER['SCRIPT_FILENAME']
.
One difference is they return different paths in included files:
__FILE__
returns path to included file (where the __FILE__
is called).$_SERVER['SCRIPT_FILENAME']
returns path to script being executed (top level script).But there could also be a difference on Linux based systems with Apache webserver where the DocumentRoot
is defined through symlinked path.
For example real path to my site is:
/mnt/data/www/mysite.org/
There is also a directory /www/
where lies this symlink:
mysite.org
pointing to /mnt/data/www/mysite.org/
.
Then DocumentRoot
(Apache's virtual host definition) is configured with symlinked path /www/mysite.org
.
In this setting in some PHP root script index.php
:
__FILE__
contains /mnt/data/www/mysite.org/index.php
- realpath$_SERVER['SCRIPT_FILENAME']
contains /www/mysite.org/index.php
- symlinked path