PHP getcwd and DirName (__file__) Different detailed explanation _php instance

Source: Internet
Author: User

__FILE__ is a magic constant that gets the full path and file name of the file. If used in the included file, the included file name is returned.

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

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

<?php
Echo 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:

<?php
Echo getcwd (). "\ n";
echo dirname (__file__). "\ n";

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

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

As you can see from the example above, GETCWD () Gets the directory of the currently running script, regardless of whether GETCWD () is in the included file or in the current execution script file, the results of the run are not changed. The __file__ gets the file name and, if used in the included file, returns the included file name and, if used directly in the current run script, returns the filename of the running script.

Hope that through this article can help everyone, thank you for your support to 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.