The problem with using the include (require) nested include file in PHP needs to be noted

Source: Internet
Author: User
/** * AUTHOR:RDP * my mail:abel1314520@gmail.com * my blog:  http://blog.teamhaka.com */


Note: The following instructions apply to include, include_once, require, require_once.

The examples are basically a.php containing nested inclusions that include c.php in b.php,b.php. The main discussion is the structural relationship between a.php and c.php. Where a.php is the entry file.

There are three options for the path when the file is included.

respectively is

An absolute path example:/aaa/bbb/ccc/c.php

Two relative paths (current directory use./) Example:./ccc/c.php

Three relative paths (the current directory does not use the./) Example: ccc/c.php

With respect to two relative paths, it is possible to take a visual note of the differences between the above (ii) and (c) relative paths. There is no./In the case of nested include files, there will be a big difference! The following article specifically explains.

One point of note using absolute paths:

There is nothing to note about direct write absolute path, but when using dirname (file) to get the absolute path of the current file, be aware that no matter whether the PHP file that writes this statement is contained by other files, dirname (file) Always points to the absolute path of the PHP file where the statement was written .

Cases:

A.php:require './b/b.php ' as the entry file;

B.php:require dirname (FILE). ' /c.php ';

Then, when executing a.php, dirname (FILE) in b.php gets the absolute path where the b.php is located, not the absolute path where the a.php is located. (In this case, b.php and c.php are in the same category B)

Two note points using relative paths (the current directory uses./):

A relative path is a directory-based baseline to determine where the files need to be included. the base directory for a relative path is the directory where the program executes the portal file, regardless of how many layers are nested.

Cases:

A.php:require './b/b.php ' as the entry file;

B.php:require './c/c.php '; Please note that this contains the C catalog when used.

So, notice that the c.php here is in the same class as the C directory and the B directory, not the C directory below the B directory. Because the inclusion in b.php uses a relative path, and the program entry is a.php, the relative path that b.php contains should be the base of the directory where the portal file a.php.

Three note points using relative paths (the current directory does not use./):

Two-step processing, first of all, the program portal file is located in the same directory as the baseline along the relative path to find, found the existence of the file contains a successful exit (and the "relative path (current directory use./)"). If you can't find it, take the second step. That is, in the directory where the PHP file that writes the Require statement is joined with the path contained in the Require, or the directory where the portal file is located, the relative path along the stitching is searched, and the file exists with success, otherwise it means that the contained file does not exist. It's easier to see examples.

Cases:

A.php:require './b/b.php ' as the entry file;

B.php:require ' c/c.php '; Please note that this is not used when the C directory is included.

First, the c/c.php is searched in the directory where the portal file a.php is located, and if so, contains success.

If not found, then the path (./b/b.php) and the path of the Require (c/c.php) in the b.php are stitched together to get the relative path after stitching./b/c/c.php. Search in the directory where the portal file a.php is located./b/c/c.php, the presence contains success, or an error occurs.

/******************* Split Line ***********************/

Summarize the nested inclusions of the relative paths slightly.

A nested inclusion of a relative path is a reference to the directory in which the program's portal file is located, along a relative path to search for the file to be included.

However, if the concept of the current directory is used in relative paths, there will be differences. That is, not using./to represent the current directory, the resulting processing is not the same.

Yes./Simply search by the directory where the portal file is located,

No./, first to search the directory where the portal file is located, do not find the case to do the stitching of the path, and then follow the stitching of the relative path to search, the benchmark is still the directory of the portal file.

Used in combination./And not used. An example :

The file structure is as follows:

/webroot

--–test.php

--–/folder2/test2.php

--–/folder2/folder3/test3.php

The test.php as the entry of the program contains the test2.php, and test2.php contains the test3.php.

The inclusion path in test2.php has the following differences:

Require (' folder2/folder3/test3.php '); That's right! Search directly in the Webroot directory where the entry path test.php is located folder2/folder3/test3.php  refer to the above "three use relative paths (not used by current directory/) Note points"


Require (' folder3/test3.php ');  That's right! Stitch the relative path and then search  for the "three Use relative path (the current directory is not used./) in the reference above"


    1. Require ('./folder2/folder3/test3.php ');  That's right!  refer to the "two use relative path (current directory use./) Note points"


    1. Require ('./test3/test3.php ');    Error! Test3 This directory does not exist under the Webroot directory where the entry test.php is located! Refer to the "two use relative path (current directory use./) Note points"

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.