[PHP] include (), require (), include_once (), require_once ()

Source: Internet
Author: User
[PHP] distinguishes include (), require (), include_once (), require_once () Author: zhanhailiang date: 2013-03-191.include () and require () (Similarly, PHP distinguishes include (), require (), include_once (), and require_once ()
Author: zhanhailiang date: 2013-03-19
1. difference between include () and require () (likewise, de_once () and require_once () can be distinguished ())

Include (), 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 (). Include () is not the case, the script will continue to run

Example 1: include () generates 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 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. Differences Between include () and include_once () (similarly, require () and require_once ())
The include_once () statement contains and runs the specified file during script execution. This action is similar to the include () statement, The only difference is that if the code in this 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 Avoid function redefinition and variable re-assignment. .
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

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
3. extended reading
  • Include
  • Require
  • Include_once
  • Require_once
  • The difference between require and include from the core php code

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.