Summarize:
1. Include or require contains other files used./or. /, the current path and the previous layer path, depending on the path of the script being run, will have the following problem.
When you write PHP programs, you often use include or require to include other files, but the files contain more files, resulting in a path problem.
The following directory:
<web> (website root directory)
├<a> folder
││
│└1.php
├<b> folder
││
│└2.php
└index.php
Now index.php in the root directory to include 1.php files in the A folder, you can use the include "./a/1.php"
While the 1.php in the 1 folder contains the 2.php in the B folder, the inclusion "is written in 1.php. /b/2.php "Can
But you know, When the index.php contains 1.php, the compilation is carried out in the index.php, that is, index.php contains the files contained in relative to the index.php, then 1.php is included in the index.php, then the relative to index.php to find Looking for the 2.php. And the above said, 1.php is written in include ". /b/2.php ", the compiled file is now relative to the site root directory (that is, relative to index.php),". /"means returning to the top-level directory and finding it, then how can it be found?
On the internet has also looked for some methods, the best way is to use absolute path method is more appropriate. You can define a single-entry file, include the file to be included, define a constant define ("__root__", DirName (__file__)), then in the process of writing the following file, only need to use absolute mode, plus __root__ on the line.
2. Include or require contains other files that are not used. /include the file name directly, depending on the path where the include files are located, such as:
<web> (website root directory)
├<a> folder
││
│└1.php
││
│└a.php
├<b> folder
││
│└2.php
└index.php
When index.php contains 1.php, the compilation is carried out in the index.php, that is, index.php contains the files contained in relative to the index.php, then 1.php is included in the index.php, if 1.php contains a.php use Require "a.php", there is no problem. using require "./a.php", there is a problem, at this time./represents the path where the index.php is located.
3. The function-loading principle of a namespace is also a containing file, but it is loaded only when it is used, and spl_autoload_register can be used to register the loading mechanism of the namespace.
4. The path of the file and the include and require are different from the file's./filename.ext or. /filename.ext or filename.ext are referenced by the directory in which the script is run (that is, the directory that is obtained by GETCWD ()), so the file operation is recommended with a relative path __file__.
$filename = ' file1.txt ';
$fp = @fopen ($filename, ' w ');
Fwrite ($fp, "\ r \ n");
Fclose ($FP);
$filename = '. /file2.txt ';
PHP file contains summary include require namespace AutoLoad spl_autoload_register Read file path