Include and require absolute paths and relative paths in PHP, too derequire
When writing a PHP program, you often need to use include or require to include other files. However, if there are too many files in each file, a path problem occurs.
The following directory:
<Web> (website root directory)
Folder <A>
│
│ Define 1.php
Folder <B>
│
│ Ipv2.php
Using index. php
Now index. php In the root directory must contain the 1. php file in the folder, use include "./A/1.php".
1. php In the 1 folder contains 2.php in the B folder. Then, write include "../B/2.PHP" in the 1. php folder.
However, you must know that when index. php contains 1. after php is compiled, it is in index. in php, that is, index. the include in php files is relative to the index. php, so 1. php is included in index. php, it is relative to index. search for php 2. php. As mentioned above, 1. in php, include ".. /B/2.php", now the compiled file is relative to the website root directory (that is, relative to index. php ),".. /"means that you still need to return to the directory at the upper level, so how can you find it.
I have also searched for some methods on the Internet. The best method is to use the absolute path method. You can define a single-entry FILE, including the FILE to be included, and define a constant define ("_ ROOT _", dirname (_ FILE __));, in the process of writing the following files, you only need to use the absolute method and add _ ROOT.