Home > Backend Development > PHP Tutorial > In-depth understanding of include/require in PHP

In-depth understanding of include/require in PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:00:07
Original
981 people have browsed it

In-depth understanding of include/require in PHP

include
1. First search for the file according to the path of the loaded file. If it is just a file name, it will be searched according to the include_path
2. If the above two addresses are not found, the directory where the script file is located and the current working directory will be called
3. If it is not found at the end, a warning will be issued. This is different from require, which will issue a fatal error
4. If path is defined. Regardless of absolute path or relative path, include_path will be invalid

require
1. Same as include method, but different error handling methods
2. When the require file is not loaded, the script will stop executing.

include_path in php

php when encountering include or require
1. First determine whether it is the right path.
Yes->Load and end
No ->Enter another logic (after multiple calls, macro expansion into _php_stream_fopen_with_path) to find this file)

<code>更详细案例说明参阅鸟哥文章: 
</code>
Copy after login

http://www.laruence.com/2010/05/04/1450.html

Conclusion It is best to use absolute paths

The difference between include and require

include(): When

  1. include introduces a file, if an error is encountered, a warning will be given and the code below will continue to run.
  2. Read and evaluate every time when executing a file
  3. It is usually placed in the processing section of the process control
  4. The PHP script file only reads the files it contains when it reads the include() statement.
  5. include() is a conditional inclusion function

require():

  1. require When importing a file, if an error is encountered, a Fatal error will be given and the code below will stop running.
  2. The file is only processed once (in fact, the file content replaces the require() statement)
  3. Usually placed at the front of the PHP script program
  4. Before the PHP program is executed, it will first read the file introduced by the require() statement , making it part of the PHP script file.
  5. require() is an unconditional inclusion function
<code><span><span><?php</span><span>//变量$ok无论是何值,1.php都会被包含进来[在PHP程序执行前,就读入require()语句]</span><span>if</span>(<span>$ok</span>){
        <span>require</span><span>'1.php'</span>;
    }
    <span>//变量$ok为真,则包含文件2.php</span><span>if</span>(<span>$ok</span>){
      <span>include</span><span>'2.php'</span>;
    }
<span>?></span></span></code>
Copy after login

Conclusion

  • include is loaded when used
  • require is loaded at the beginning
  • _once suffix indicates that the loaded one is not loaded
  • If it is possible to execute it multiple times code, it is more efficient to use require()
  • If you read a different file each time you execute the code, or there is a loop that iterates through a set of files, use the include() statement
').addClass(' pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced an in-depth understanding of include/require in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template