Home > Article > Backend Development > Detailed explanation of the difference between php getcwd and dirname(__FILE__)
This article mainly introduces the relevant information about the difference between php getcwd and dirname(__FILE__). Friends who need it can refer to it
__FILE__ is a magic constant, used to obtain the complete path of the file. and file name. If used within an included file, returns the name of the included file.
Let’s introduce the difference between getcwd and dirname(__FILE__) through examples.
The code of file/folder/random/foo.php is as follows:
<?php echo getcwd() . "\n"; echo dirname(__FILE__) . "\n" ; echo "-------\n"; include 'bar/bar.php';
The code of file/folder/random/bar/bar.php The code is as follows:
<?php echo getcwd() . "\n"; echo dirname(__FILE__) . "\n";
Run the code/folder/random/foo.php, the result is:
/folder/random /folder/random ------- /folder/random /folder/random/bar
As can be seen from the above example, getcwd() obtains the directory of the currently running script. No matter whether getcwd() is in the included file or the currently executing script file, nothing will happen to the running result. Variety. __FILE__ obtains the file name. If used in an included file, the included file name is returned. If used directly in the currently running script, the file name of the running script is returned.
I hope this article can help everyone, thank you for your support of this site!
The above is the detailed content of Detailed explanation of the difference between php getcwd and dirname(__FILE__). For more information, please follow other related articles on the PHP Chinese website!