The correct usage of the require () file you should know in PHP,

Source: Internet
Author: User

The correct usage of the require () file you should know in PHP,

When I used to look at some PHP framework source code, it was strange that dirname (_ FILE _) will be used to piece together the FILE path during FILE inclusion. I don't know what the benefits of doing so, later, I finally found out why.

Let's take a simple example:

There are three PHP files, a, B, and c. A. php is in the root directory of the website, and B. php is in the B folder -- B/B. php, and c. php are in the c folder -- c/c. php. Some confusion? You can see at a glance:

Both a. php and B. php contain c. php. Finally, c. php contains a php file named d/d. php In the d folder.

Let's first look at a. php:

<?php   $file_name = 'a.php';  echo "this is a.php";  echo "

The code is very simple. After the output is printed, it contains c/c. php. Then, we need to check c/c. php:

<?php   $c_file_name = 'c.php';  echo 'this is c.php, is required by ' . $file_name;  echo "

Print the output "this is c. php, is required by a. php", $ file_name is the variable defined in a. php. At the end, it contains d. php. Because the d folder is located on the top of the current c. php file, we naturally write the path as "../d. php" according to common sense ". But unfortunately, an error will be reported. The reason is that c. php, and then include other files. The path is relative to the parent file of the outermost layer, that is, relative to. php can be understood as because you are included by me, so you must take me as the standard. The principle is actually very simple: You can put require ('C/c. php '); c/c. PHP file code, so that our. php looks like this:

<?php   $file_name = 'a.php';  echo "this is a.php";  echo "

At this point, you can see that when we want to include the d/d. php file, is the path just now incorrect? Because, in a. php code, we are relative to the. php file. Of course, the path should be require ('d/d. php. The modified code is as follows:

<?php   $file_name = 'a.php';  echo "this is a.php";  echo "

At this time, you have not realized your deep understanding. You need to look at it. Let's look at B/B. php again:

<?php   $file_name = 'b.php';  echo "this is b.php";  echo "

You don't need to explain it. It's okay, but when you set require ('.. /c. php '); change to c/c. when using the code in php, you will find the problem. Note that we have modified c/c. php code, put require ('.. /d. php '); changed to require ('d/d. php '); check the following code:

<?php   $file_name = 'b.php';  echo "this is b.php";  echo "

Then, relative to B/B. for php, require ('d/d. php '); the path is incorrect. It should be require ('.. /d. php. You can modify c/c. the require path in php is incorrect. After you change it, B/B. php can run normally, but a/. php doesn't work anymore. Do they share c/c. php: What should I do if I start the whole system.

At this time, we will go back to the dirname (_ FILE _) mentioned at the beginning of the article. This is a good thing and can completely solve the above problems. With it, you don't need to worry about which file contains your file and which path it is under. You don't need to worry about the level of the parent file, because, dirname (_ FILE _) can specify a path relative to the current FILE. In other words, we need to replace the require path in our c/c. php:

<?php   $c_file_name = 'c.php';  echo 'this is c.php, is required by ' . $file_name;  echo "

Here, we only need to take c/c. php as a reference. Relative to it, d/d. php is on the upper layer. In this way, there is only one standard, that is, take me as the standard. Whether you include me or include me, I will only take my own as the standard, the file I want to include is only relative to myself.

If dirname (_ FILE _) is not clear, google is recommended.

Now, PHP Technology is over. Please leave a message if you have any questions or errors. In other words, this is my first standard technology blog. The first is hydrology, and the second is quasi-technology. Today I finally wrote a technology article, which is also published by ou.

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.