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

The difference between PHP $_SERVER[SCRIPT_FILENAME] and __FILE__

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-08 09:19:41
Original
1063 people have browsed it

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__;
?>
Copy after login

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 
Copy after login

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';
?>
Copy after login

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 
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template