Home  >  Article  >  Backend Development  >  [PHP] Distinguish between include(), require(), include_once(), require_once()_PHP tutorial

[PHP] Distinguish between include(), require(), include_once(), require_once()_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:09:36898browse

1.The difference between include() and require() (in the same way, include_once() and require_once() can be distinguished)

include(), require() statements include and run the specified file. The two structures are identical except for how they handle failure.
include() produces a warning and require() results in a fatal error. In other words, use require() if you want to stop processing the page if a missing file is encountered. This is not the case with include(), the script will continue to run
Example 1: include() generates a Warning and require() results in 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 1
zhanhailiang@linux-06bq:~> php -r "require('a.php');"
Warning: require(a.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 1
2.The difference between include() and include_once() (Similarly, require() and require_once() can be distinguished)
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 is already included, it will not be included again. As the name of this statement implies, it will only be included once.
include_once() should be used when the same file may be included more than once during script execution, and you want to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment.
The return value is the same as include(). If the file is included, this function returns TRUE.
Example 1: include() will include the specified file multiple times, but include_once() will not.
zhanhailiang@linux-06bq:~> cat a.php
echo '1'.PHP_EOL;
zhanhailiang@linux-06bq:~> php -r "include('a.php');include('a.php');"
1
1
zhanhailiang@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
echo '1'.PHP_EOL;
function test() {}
zhanhailiang@linux-06bq:~> 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 4
zhanhailiang@linux-06bq:~> php -r "include_once('a.php');include_once('a.php');"
1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477641.htmlTechArticle1. The difference between include() and require() (in the same way, include_once() and require_once() can be distinguished) include(), require() statements include and run the specified file. In addition to how to handle failures...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn