This article mainly introduces the correct use of the require () file that you should know in PHP. For more information, see
This article mainly introduces the correct use of the require () file that you should know in PHP. For more information, see
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 ""; 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:
<? Php $ c_file_name = 'C. php '; echo' this is c. php, is required '. $ 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:
<? Php $ file_name = 'a. php'; echo "this is. php "; echo" "; // require ('C/c. php '); $ c_file_name = 'C. php '; echo' this is c. php, is required '. $ 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:
<? Php $ file_name = 'a. php'; echo "this is. php "; echo" "; // require ('C/c. php '); $ c_file_name = 'C. php '; echo' this is c. php, is required '. $ 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:
<? 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:
<? 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 '. $ 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:
<? Php $ c_file_name = 'C. php '; echo' this is c. php, is required '. $ 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.