Differences between the PHP include () and require () methods

Source: Internet
Author: User

This article summarizes the differences between the PHP include () and require () methods that contain external files. Basically, the load fails the processing method, the performance, and the use of elasticity are different.

PHP include () and require () are two ways to include external files, and for these two methods, many beginners may not be very clear. Here's a summary of the differences between PHP include () and require ():

1: Load failure is handled differently:

The include () generates a warning, while 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

It is recommended to use require_once () and include_once () to detect if a file has duplicate inclusions.

2.php Performance

For include (), files are read and evaluated each time the include () is executed;

for require (), the file is processed only once (in fact, the file contents replace the Require () statement).

This means that if you have code that contains one of these directives and code that might execute multiple times, using require () is a high efficiency.

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.

Require use methods such as require ("./inc.php");. Usually placed at the top of the PHP program, the PHP program before execution, will read into the require specified in the file, so that it becomes a part of the PHP Program 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.

Require is included even when the condition bit is false, and the include is only executed when the position is changed.

The require_once () statement includes and runs the specified file during script execution. This behavior is similar to the Require () statement, except that if the code in the file is already included, it will not be included again. The action of the Require_once () function and the require () are almost identical

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

The role of require_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 differences between the PHP include () and require () methods.

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.