Home > Backend Development > PHP Tutorial > Detailed explanation of the difference between php getcwd and dirname(__FILE__)

Detailed explanation of the difference between php getcwd and dirname(__FILE__)

不言
Release: 2023-03-29 18:04:01
Original
1378 people have browsed it

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 &#39;bar/bar.php&#39;;
Copy after login

The code of file/folder/random/bar/bar.php The code is as follows:

<?php
echo getcwd() . "\n";
echo dirname(__FILE__) . "\n";
Copy after login

Run the code/folder/random/foo.php, the result is:

/folder/random
/folder/random
-------
/folder/random
/folder/random/bar
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template