The difference between include and require

Source: Internet
Author: User

First prepare a file to include a.php, the file content is as follows

$a = 1;?>

Test require_once, the content is as follows

<? PHP  for ($i$i<=10; $i++  )    {require_once(' a.php ')    ; Echo $a+ +;}

The output is as follows, because only the first containment is valid, and subsequent inclusions are not valid.

12345678910

Test require, the content is as follows

<? PHP  for ($i$i<=10; $i++  )    {require(' a.php ')    ; Echo $a+ +;}

The output is as follows, each time to execute the inclusion, so the output is 1

1111111111

Change require and require_once to include and include_once perform the same effect.

2. The difference between include and require is that the error is not handled in the same way

Require

<? PHP require (' b.php '); Echo "Contiue code \ n";

The execution result is as follows, continue code this sentence does not have the output, indicates the require contains the file if encounters the error, the later code does not carry out the

PHP Warning:  requirefile or directory in/home/lxy/htdocs/test.php on line 2php Fatal error
    :  require(): Failed opening required ' b.php ' (include_path= '.: ') in/home/lxy/htdocs/test.php on line 2

Include

<? PHP include (' b.php '); Echo "Contiue code \ n";

The execution results are as follows, continue code outputs the

PHP Warning:  includefile or directory in/home/lxy/htdocs/test.php on line 3contiue code PHP Wa Rning:  include for inclusion (include_path= '.: ') in/home/lxy/htdocs/test.php on line 3

The difference between include and require

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.