[PHP] differentiate include (), require (), include_once (), require_once () _php Tutorial

Source: Internet
Author: User
The difference between 1.include () and require () (similarly distinguishable include_once () and require_once ()) include (), the Require () statement contains and runs the specified file. These two structures are exactly the same in addition to how they handle failures. The include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing a page when you encounter a lost file, use require (). The include () is not so, the script continues to run the example 1:include () produces a warning and require () causes a fatal error. Zhanhailiang@linux-06bq:~> php-r "include (' a.php '); Warning:include (a.php): Failed to open stream:no such file or directory in Command line code on line 1 Warning:include ( ): Failed opening ' a.php ' for inclusion (include_path= '.:/ Usr/local/services/phplib/src:/usr/local/services/phplib/inc:/usr/local/services/php/lib/php ') in Command line Code on line 1zhanhailiang@linux-06bq:~> php-r "require (' a.php ');" Warning:require (a.php): Failed to open stream:no such file or directory Command line code on line 1 Fatal error:requ IRE (): Failed opening required ' a.php ' (include_path= '.:/ Usr/local/services/phplib/src:/usr/local/services/phplib/inc:/usr/local/services/php/lib/php ') in Command line Code on line 12.include () and IncludThe difference between e_once () (the same can be distinguished require () and require_once ()) include_once () statements include and run the specified file during script execution. This behavior is similar to the include () statement, except that if the code in the file is already contained, it will not be included again. As the name of this statement implies, it is included only once. Include_once () should be used when the same file is likely to be included more than once during script execution, to ensure that it is included only once to avoid problems such as function redefinition, variable re-assignment, and so on. The return value is the same as include (). If the file is already contained, this function returns TRUE. Example 1:include () will contain the specified file multiple times, and include_once () will not. Zhanhailiang@linux-06bq:~> Cat a.php Php-r "include (' a.php '); include (' a.php ');" 11zhanhailiang@linux-06bq:~> php-r "include_once (' a.php '); include_once (' a.php ');" 1 www.2cto.com Example 2:include_once () avoids function redefinition. Zhanhailiang@linux-06bq:~> Cat a.php Php-r "include (' a.php '); include (' a.php ');" 1 Fatal error:cannot redeclare test () (previously declared in/home/zhanhailiang/a.php:4) in/home/zhanhailiang/a.php on Line 4zhanhailiang@linux-06bq:~> php-r "include_once (' a.php '); include_once (' a.php ');" 1

http://www.bkjia.com/PHPjc/477641.html www.bkjia.com true http://www.bkjia.com/PHPjc/477641.html techarticle the difference between 1.include () and require () (similarly distinguishable include_once () and require_once ()) include (), the Require () statement contains and runs the specified file. These two structures, in addition to how to deal with the failure of ...

  • Related Article

    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.