PHP GETCWD differs from dirname (__file__)

Source: Internet
Author: User
This article mainly introduces the PHP getcwd and dirname (__file__) different details of the relevant information, the need for friends can refer to the following

__FILE__ is a magic constant used to get the full path and file name of a file. If used in the included file, returns the file name that is included.

Below we introduce the difference between GETCWD and DirName (__file__) by an example.

The code for the file/folder/random/foo.php is as follows:

<?phpecho GETCWD (). "\ n"; Echo dirname (__file__). "\ n"; echo "-------\ n"; include ' bar/bar.php ';

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

<?phpecho GETCWD (). "\ n"; Echo dirname (__file__). "\ n";

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

/folder/random/folder/random-------/folder/random/folder/random/bar

As can be seen from the above example, GETCWD () Gets the directory that is currently running the script, regardless of whether GETCWD () is inside the contained file or in the currently executing script file, the result of the run will not change. And __file__ gets the file name, if used in the included file, returns the included file name, and if used directly in the current run script, returns the file name of the running script.

Hope that through this article can help everyone, thank you for the support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.