In PHP you should know the correct usage of the Require () file included

Source: Internet
Author: User
Tags php file php and php code php framework relative require root directory

Before looking at some PHP framework source code, very strange in the file contains, will use DirName (__file__) to patchwork file path, do not know what the benefits, then finally found the reason.

Let's look at a simple example:

There are a,b,c three PHP files. A.php in the site root directory, b.php under the B folder--b/b.php,c.php under the C folder--c/c.php. Some confusion? Look at the picture is at a glance:

A.php and b.php all contain c.php, and finally c.php contains a PHP file--d/d.php under the D folder.

Let's look at a.php first:

1 2 3 4 5 6 <?php $file _name = ' a.php '; echo "This is a.php"; echo "

Very simple code, printed after the output, contains the c/c.php, and then we need to look at c/c.php:

1 2 3 4 5 6 <?php $c _file_name = ' c.php '; Echo ' This is c.php, are required by '. $file _name; echo "

PrintOut "This is c.php, are required by a.php", $file _name are variables defined in a.php. In the end, it contains the d.php. Because the D folder is on the top level of the current c.php file, as a matter of common sense, we will naturally write the path as "... /d/d.php ". But unfortunately, it will be an error. The reason is that in the included file, such as c.php, and then to include other files, the path is relative to the outermost parent file, that is, relative to a.php, you can understand that because you are included in me, so you have to take me as the subject. Looks very iffy, the principle is actually very simple: you can put require (' c/c.php '); Look at the code in the c/c.php file so that our a.php looks like this:

1 2 3 4 5 6 7 8 9 10 <?php $file _name = ' a.php '; echo "This is a.php"; echo "

So, as you can see, when we're going to include d/d.php files, is the path just wrong? Because, now is in the a.php code, we are relative to the a.php file, of course, the path should be require (' d/d.php '); That's right. We modify the code as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13-14 <?php $file _name = ' a.php '; echo "This is a.php";   echo "

At this time, you have not realized the meaning, need to look down, we look at b/b.php:

1 2 3 4 5 6 7 <?php $file _name = ' b.php '; echo "This is b.php";   echo "

No need to explain, no problem, but when you put require ('. /c/c.php '); Instead of c/c.php inside the code, you will find the problem, note that we have just modified the code in c/c.php, the Require ('. /d/d.php '); Changed into the Require (' d/d.php '); Look at the following included code:

1 2 3 4 5 6 7 8 9 10 11 <?php $file _name = ' b.php '; echo "This is b.php";   echo "

So, relative to b/b.php, require (' d/d.php '); The path is wrong, should be require ('. /d/d.php '); That's right. You go back to modify the c/c.php in the Require path, but not ah, you changed, b/b.php can be normal operation, but a/a.php again, is not, they share c/c.php, pull a body, how to do it.

This time, we return to the beginning of the article mentioned dirname (__file__), this is a good thing, can completely solve the above problems. With it, you can not care which file contains your file, under which path, do not need to worry about the level of the parent file, because, DirName (__file__) can be relative to the current file specified path. In other words, we need to change the require path in our c/c.php to:

1 2 3 4 5 6 7 8 <?php $c _file_name = ' c.php '; Echo ' This is c.php, are required by '. $file _name;   echo "

Here, we just need to take c/c.php as a reference, and in contrast to it, d/d.php is on the upper level. In this way, there is only one standard, that is, to me, whether you contain me, or he contains me, I am only the subject of my own, I want to include the document only relative to my own.

For DirName (__file__) do not understand the same repair, please Google, very simple.

OK, PHP technology to share the end, there are any questions or errors, please leave a message. In other words, this is my first standard technology blog. The first is hydrology, the second is quasi technology, today finally wrote a piece of technology, Europe also.

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.