Similarities and differences between require and include

Source: Internet
Author: User
The basic difference between require and include in PHP is that in general PHP development, the functions of require and include are roughly the same, but there are some differences. please refer to the following description. The basic difference between require and include in PHP is that in general PHP development, the functions of require and include are roughly the same, but there are some differences. please refer to the following description.
First point
: Require () and include () are identical in all aspects except how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when a file is lost, use require () to directly stop the script. This is not the case with include (). The script will continue to run, but a Notice-level error will be thrown. Make sure that the appropriate include_path is set.

That is to say, when parsing the program, it reads the require file instead of the file to be parsed. if you cannot read the file to be require, you cannot perform the next action. Therefore, improper inclusion may lead to program files. it is better to use require.

Second point
: Require () will contain files in any way, and include () can selectively include:
If (FALSE ){
Require ('A. php ');
}
If (FALSE ){
Include ('B. php ');
}
?>

For example, in the above code, a. php will be included, and B. php will not. Require is similar to a pre-scan. during program execution, The require file will be executed only once, whether in the function or outside the function. The include statement calls the file once every execution. that is, after this execution, the file will be executed again next time. You should understand the difference :)


You can also see the difference between require_once and include_once.
The require_once () statement includes and runs the specified file during script execution. This behavior is similar to the require () statement. The only difference is that if the code in the file has been included, it will not be included again.
The include_once () statement includes and runs the specified file during script execution. This behavior is similar to the include () statement. The only difference is that if the code in the file has been included, it will not be included again. As the statement name implies, it will only be included once.

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.