[PHP] differentiate include (), require (), include_once (), require_once ()
Source: Internet
Author: User
1. The difference between include () and require () (likewise, you can distinguish between include_once () and require_once () include (). The require () statement contains and runs the specified file. These two structures are identical except for how to handle failures. Include () generates a warning and require () causes... "/> <scriptt 1. the difference between include () and require () (likewise, you can distinguish between include_once () and require_once () include (). The require () statement contains and runs the specified file. These two structures are identical except for 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 (). This is not the case with include (). The script will continue to run Example 1: include () to generate a Warning and require () will cause a Fatal error. Zhanhailiang @ linux-06bq: ~> Php-r "include ('A. php '); "Warning: include (. php): failed to open stream: No such file or directory in Command line code on line 1 Warning: include (): Failed opening 'A. php 'for declaration (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 (. php): failed to open stream: No such file or directory in Command line code on line 1 Fatal error: require (): 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. the difference between include () and include_once () (likewise, require () and require_once () include _ The once () statement contains 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. Include_once () should be used when the same file may be contained more than once during script execution, to ensure that it is only included once to avoid function redefinition, variable re-assignment and other issues. The returned value is the same as include. If the file has been included, this function returns TRUE. Example 1: include () contains the specified file multiple times, but include_once () does 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 () avoid function redefinition. Zhanhailiang @ linux-06bq: ~> Cat a. php Php-r "include ('A. php '); include ('A. php '); "1 Fatal error: Cannot redeclare test () (previusly declared in/home/zhanhailiang/. php: 4) in/home/zhanhailiang/. php on line 4zhanhailiang @ linux-06bq: ~> Php-r "include_once ('A. php'); include_once ('A. php');" 1
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.