Include and require absolute paths and relative paths in PHP, too derequire

Source: Internet
Author: User

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.

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.