The difference between PHP include and require methods

Source: Internet
Author: User
PHP's include () and require() are two ways to include external files, and for the difference between these two methods, many beginners may not be very clear. Here's a summary of PHP include () and require() The difference:
1: Load failure is handled differently:
The include () generates a warning and require() causes a fatal error (an error occurred and the script stopped executing)
require(): If the file does not exist, a fatal error is reported. The script stops executing
Include (): If the file does not exist, it will give a warning, but the script will continue to execute
In particular, it is important to note that when the include () file does not exist, the script continues to execute, which only appears before PHP 4.3.5
Recommended Use require_once () and include_once () can detect if a file has duplicate inclusions.
2.php Performance
For include (), files are read and evaluated each time the include () is executed;
And for require(), the file is processed only once (in fact, the contents of the file replace the require() statement).
This means that if you have code that contains one of these directives and code that might execute multiple times, use the require() high efficiency ratio.
On the other hand, if you read a different file each time you execute the code, or you have a loop that passes through a set of file iterations, use the Include (),
Because you can set a variable for the file name you want to include, use this variable when the parameter is include ().
3. Two ways to provide different elasticity of use.
requireThe use of methods such as require("./inc.php");. Usually put in the front of the PHP program, the PHP program before execution, will be read in the first requireThe file that you specify to make it a part of the PHP program's Web page.
Include usage methods such as include ("./inc/.php");. is usually placed in the Process Control section. The PHP Program page reads the include file when it is read. This way, you can simplify the process of executing the program.
requireIt is included even when the condition bit is false, and the include is only executed when the position is changed.
requireThe _once () statement includes and runs the specified file during script execution. This behavior and require() statement is similar, the only difference is that if the code in the file is already included, it will not be included again. requireThe function of _once () functions and require() is almost the same
The include_once () statement includes and runs the specified file during script execution. This behavior is similar to the include () statement, except that if the code in the file is already included, it will not be included again. The role of the include_once () function and the include () are almost identical
requireThe role of _once is to check whether the file has been loaded before, and if it is not loaded, it will not load if it is loaded, such as a file defines a type if the file is loaded two times an error occurs
These are the PHP include () and requireThe difference between the () method.

The above describes the PHP include and require method of the difference, including the require aspect of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.