Which way to get the PHP self script name is the fastest?

The result shows that __FILE__ is the fastest, so use that when you don't have to use any other method to find the script's self. __FILE__ is a built in constant show the location of the running script. Note that each one of those ways can return different things(like PHP_SELF is different than REQUEST_URI when used some mod_rewrite) but most times those have the same usage, refer to self.
Here is the code for the result:

  $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
$i = 1000;
while($i){
	__FILE__;
	--$i;
}
  $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo '<br />__FILE__;',bcsub($endtime,$starttime,6);
 
 
 
  $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
$i = 1000;
while($i){
	$_SERVER['PHP_SELF'];
	--$i;
}
  $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo '<br />','$_SERVER["PHP_SELF"];',bcsub($endtime,$starttime,6);
 
  $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
$i = 1000;
while($i){
	$_SERVER['SCRIPT_NAME'];
	--$i;
}
  $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo '<br />','$_SERVER["SCRIPT_NAME"];',bcsub($endtime,$starttime,6);
 
  $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
$i = 1000;
while($i){
	$_SERVER['REQUEST_URI'];
	--$i;
}
  $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo '<br />','$_SERVER["REQUEST_URI"];',bcsub($endtime,$starttime,6);
 
 
  $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
$i = 1000;
while($i){
	getenv('REQUEST_URI');
	--$i;
}
  $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo '<br />','getenv("REQUEST_URI");',bcsub($endtime,$starttime,6);
 
  $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
$i = 1000;
while($i){
	getenv('SCRIPT_NAME');
	--$i;
}
  $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo '<br />','getenv("SCRIPT_NAME");',bcsub($endtime,$starttime,6);
 
 
    $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
$i = 1000;
while($i){
	getenv('SCRIPT_URL');
	--$i;
}
  $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo '<br />','getenv("SCRIPT_URL");',bcsub($endtime,$starttime,6);

[...] WebDevLogs ? Which way

[...] WebDevLogs ? Which way to get the PHP self script name is the fastest? (tags: Tech WebDev PHP Performance Tips) [...]

funny :P

php blog's picture

funny :P

__FILE__ given included script name

manish sharma's picture

__FILE__ given included script name if use in include file or class as a function...

pls check again,,and give me answer as soon as possible.

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Use <fn>...</fn> to insert automatically numbered footnotes.

More information about formatting options

What is 77 + 21?
To combat spam, please solve the math question above.
Honey Pot that kill bots