Introduction to include () and require () in php

Source: Internet
Author: User
This article describes in detail the file call tests in php, including include (), require (), include_once (), and require_once. Include

This article describes in detail the file call tests in php, including include (), require (), include_once (), and require_once.

Use of Include, can contain the same file multiple times,The code is as follows:

  1. Include 'demo1. php ';
  2. Include 'demo1. php ';
  3. Include 'demo1. php ';
  4. ?>

The output result is as follows:

  1. Same content
  2. ----
  3. Same content
  4. ----
  5. Same content

Include_once is similar to include, but multiple calls only contain the same file once.The code is as follows:

  1. Include_once 'demo1. php ';
  2. Include_once 'demo1. php ';
  3. Include_once 'demo1. php ';
  4. ?>

The result is as follows:

The require () statement contains and runs the specified file. the code is as follows:

  1. Require 'demo1. php ';
  2. Require 'demo1. php ';
  3. Require 'demo1. php ';
  4. ?>

The result is as follows:

  1. Same content
  2. ----
  3. Same content
  4. ----
  5. Same content

The require_once () statement contains and runs the specified file during script execution, but does not contain the same file repeatedly. the code is as follows:

  1. Require_once 'demo1. php ';
  2. Require_once 'demo1. php ';
  3. Require_once 'demo1. php ';
  4. ?>

The output result is as follows:

Difference Between include and require

If there is other code behind Include, when an error occurs in calling include, the subsequent code will continue to be executed, but require will not. Include will give a warning when calling a non-existent file, however, the code is as follows:

  1. Include 'demo11.php ';
  2. Echo ('this is demo13.php ');
  3. ?>

The output result is as follows:

  1. Warning: include (demo111.php) [function. include]: failed to open stream: No such file or directory in D: AppServwwwBasic7demo13. php on line 2
  2. Warning: include () [function. include]: Failed opening 'demo111. php' for future Sion (include_path = '.; C: php5pear') in D: AppServwwwBasic7demo13. php on line 2
  3. This is demo13.php

When Require calls a non-existent file, it will give an error and abort code execution. the code is as follows:

  1. Require 'demo111. php ';
  2. Echo ('this is demo14.php ');
  3. ?>

The output result is as follows:

  1. Warning: require (demo111.php) [function. require]: failed to open stream: No such file or directory in D: AppServwwwBasic7demo14. php on line 2
  2. Fatal error: require () [function. require]: Failed opening required 'demo111. php' (include_path = '.; C: php5pear') in D: AppServwwwBasic7demo14. php on line 2

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.