Home  >  Article  >  Backend Development  >  The difference between PHP $_SERVER[SCRIPT_FILENAME] and __FILE__

The difference between PHP $_SERVER[SCRIPT_FILENAME] and __FILE__

WBOY
WBOYOriginal
2016-08-08 09:19:41999browse

PHP $_SERVER['SCRIPT_FILENAME'] and __FILE__

Normally, PHP $_SERVER['SCRIPT_FILENAME'] and __FILE__ will return the full path (absolute path) and file name of the PHP file:

<?php
echo 'SCRIPT_FILENAME 为:',$_SERVER['SCRIPT_FILENAME'];
echo '<br />';
echo '__FILE__ 为:',__FILE__;
?>

The above test code Copy to test.php and access the file (http://127.0.0.1/php/test.php), and get the following results:

SCRIPT_FILENAME 为:E:/web/html/php/test.php
__FILE__ 为:E:\web\html\php\test.php 

Tips: When testing on the Windows platform, path separators may appear in the results as shown above. nuances.

The difference between $_SERVER['SCRIPT_FILENAME'] and __FILE__

Although $_SERVER['SCRIPT_FILENAME'] is very similar to __FILE__, there are still subtle differences between the two when the file is included or require.

Copy the above test code to E:webhtmlphpcommoninc.php, and then include inc.php in the test.php file just now:

<?php
include 'common/inc.php';
?>

When you access the test.php file again at this time, the output result is:

SCRIPT_FILENAME 为:E:/web/html/php/test.php
__FILE__ 为:E:\web\html\php\common\test.php 

You can see both The difference is: $_SERVER['SCRIPT_FILENAME'] reflects the absolute path and file name of the currently executing program; __FILE__ reflects the absolute path and file name of the original file (included file).

The above introduces the difference between PHP $_SERVER[SCRIPT_FILENAME] and __FILE__, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn