• The four methods are as follows:
    • Incude “File path to load “;
    • Include_once “File path to load “;
    • Require “file path to load “;
    • Require_once “file path to load “;

  • The difference between the four ways

    • Inchude: loads the file every time (possibly repeatedly), and if it fails, continues after an error;
    • Include_once: load only once (not repeatedly), if the load fails, continue after an error;
    • Require: loads the file every time (possibly repeatedly), terminates the program after an error is reported if the load fails;
    • Require_once: loads only once (not repeatedly). If loading fails, the program is terminated after an error is reported.

    In general, if the loaded file content is a prerequisite for subsequent code execution, then require loading should be used.

If the contents of the file being loaded need (or are only allowed) to appear once, you should use ‘XXXX once”.

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, "> <title>Document</title> </head> <body> <? php $file = __DIR__ . '/lib/nav.html'; Include_once (include_once, include_once, include_once) include_once $file; include_once $file; // avoid repeated import and only take effect once // require_once fails to load and terminates the program. require_once $file; require_once $file; ? > </body> </html>Copy the code

  • The path “file to load” above is a file path, can be a relative path, can be an absolute (physical) path, or direct file name:
    • Relative path: ‘./ file name ‘; ‘./dir/ file name ‘; ‘./dir/dir/ file name ‘; ‘.. /.. /dir/ file name ‘; .

    • Absolute (physical) path: ‘c: / itcast/class/php66 day4 / filename’; // window system ‘/path1/path2/ file name ‘; // Inux, Unix and other systems……


  • How to obtain a physical path (absolute path) :

__DIR__ : indicates the path of the current file, from which an absolute path can be constructed. Getcwd () : indicates the path of the web page being visited. It can also build an absolute path.


  • Non-relative non-absolute path: not recommended!
    • The format is include file name.
    • The include_path option in php. ini is used to search for the file in the following order: 1. 2. If it was not found in the previous step, look for the file in the current working directory (obtained by getcwd()); 3. If it was not found in the previous step, look in the directory (__DIR__) where the statement is currently loaded. 4. If the previous step is still not found, an error is reported.

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, "> <title>Document</title> </head> <body> <? PHP echo '__DIR__ : ', __DIR__; $file = __DIR__ . '/lib/nav.html'; Echo '<br>__DIR__ want to load path is: ', $file; include $file; Echo '<hr>'; Echo 'getcwd() ', getcwd(); $file1 = getcwd() . '/lib/nav.html'; Echo '<br>getcwd() want to load path: ', $file1; include $file1; Echo '<hr>'; Echo '__DIR__ 'differs from getcwd() : '; include 'lib/page.php'; ? > </body> </html>Copy the code
  • page.php:
<? PHP echo '<br>__DIR__ '; echo '<br>' . __DIR__; Echo '<br>getcwd() : echo '<br>getcwd() : echo '<br>' . getcwd(); ? >Copy the code