Home > php教程 > php手册 > Problems with include and require absolute paths and relative paths in PHP

Problems with include and require absolute paths and relative paths in PHP

WBOY
Release: 2016-10-19 10:19:36
Original
2026 people have browsed it

When writing PHP programs, you often use include or require to include other files. However, when there are too many files included in each file, path problems will occur.

The following directory:

(website root directory)
Folder
│ │
│ └1.php
Folder
│ │
│ └2.php
└index.php

Now the index.php in the root directory needs to include the 1.php file in the A folder, just use include "./A/1.php"

And 1.php in folder 1 includes 2.php in folder B, then write include "../B/2.PHP" in 1.php

But you must know that when index.php includes 1.php, compilation is performed in index.php, that is, the include in the file included in index.php is relative to index.php, then 1.php is included in index.php, then we need to find 2.php relative to index.php. As mentioned above, what is written in 1.php is include "../B/2.php". Now the compiled file is relative to the root directory of the website (that is, relative to index.php), and "../" means Then we have to go back to the upper level directory to search, so how can we find it?

I have also searched for some methods on the Internet, and the best way is to use the absolute path method. You can define a single entry file, include the files to be included, and define a constant define("__ROOT__",dirname(__FILE__));, then in the process of writing subsequent files, you only need to use the absolute method and add __ROOT__ That's it.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template