Correct use of the require () file you should know in PHP

Source: Internet
Author: User

Correct use 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:

1

2

3

4

5

6

<? Php

$ File_name = 'a. php ';

Echo "this is a. php ";

Echo "

Require ('C/c. php ');

?>

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

1

2

3

4

5

6

<? Php

$ C_file_name = 'C. php ';

Echo 'this is c. php, is required by '. $ file_name;

Echo "

Require ('../d. php ');

?>

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:

1

2

3

4

5

6

7

8

9

10

<? Php

$ File_name = 'a. php ';

Echo "this is a. php ";

Echo "

// Require ('C/c. php ');

$ C_file_name = 'C. php ';

Echo 'this is c. php, is required by '. $ file_name;

Echo "

Require ('../d. php ');

?>

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:

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 "

 

// Require ('C/c. php ');

$ C_file_name = 'C. php ';

 

Echo 'this is c. php, is required by '. $ file_name;

Echo "

 

Require ('d/d. php ');

?>

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

1

2

3

4

5

6

7

<? Php

$ File_name = 'B. php ';

Echo "this is B. php ";

Echo "

 

Require ('../c. php ');

?>

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:

1

2

3

4

5

6

7

8

9

10

11

<? Php

$ File_name = 'B. php ';

Echo "this is B. php ";

Echo "

 

// Require ('../c. php ');

$ C_file_name = 'C. php ';

Echo 'this is c. php, is required by '. $ file_name;

Echo "

Require ('d/d. php ');

?>

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:

1

2

3

4

5

6

7

8

<? Php

$ C_file_name = 'C. php ';

 

Echo 'this is c. php, is required by '. $ file_name;

Echo "

 

Require (dirname (_ FILE _). '/../d. php ');

?>

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.