The difference between include and require in PHP

Source: Internet
Author: User
Tags pear

The include and require are a function that imports PHP into the current PHP file, but there are two different functions.

The difference between the two is:

(1) Include, each acquisition, to go to the file for access and evaluation, but require, the file is only processed once, the contents of the file will be replaced by the require, so that if each time to read different code, include is more advantageous, However, if you read the same code more than once, the require will be much more efficient.

If require ("1.php") is placed at the top of the PHP file, the contents of the file will be replaced by the require before the PHP file executes, while the include is required to be read.

To summarize one thing:

Include is used to read,

Require is used before reading,

_once is used if the file is the same, then only gets once

(2) Two at run time, if you encounter a file that does not exist.

 1  <? php  2   r.php  3   $a  = 1; 4  $ A /0;  5 ? 
1 <? PHP 2 // i.php 3     $b = 2; 4     Echo $b /0; 5 ?>

Two PHP files were prepared, one is r.php and the other is i.php

First verify the Require:

1<?PHP2     require"R.php";3     require"A.php";4     Echo $a;5?>6 /*7 8 warning:division by zero in C:\xampp\htdocs\test\r.php on line 39 Ten Warning:require (a.php): Failed to open stream:no such file or directory in C:\xampp\htdocs\test\p.php on line 3 One  A Fatal Error:require (): Failed opening required ' a.php ' (include_path= '.; C:\xampp\php\PEAR ') in C:\xampp\htdocs\test\p.php on line 3 - */

There is no a.php so the program terminates here

Then verify the following include:

1<?PHP2     include"R.php";3     include"A.php";4     Echo $a;5?>6 /*7 8 warning:division by zero in C:\xampp\htdocs\test\r.php on line 39 Ten Warning:include (a.php): Failed to open stream:no such file or directory in C:\xampp\htdocs\test\p.php on line 3 One  A warning:include (): Failed opening ' a.php ' for inclusion (include_path= '.; C:\xampp\php\PEAR ') in C:\xampp\htdocs\test\p.php on line 3 - 1 - */

The program runs smoothly and can output a $ A result, although no corresponding file can be found

We then verify that the require is replaced by the contents of the file (whether it is an unconditional reference)

1 <? PHP 2     if (0) {3         require "r.php"; 4     }5     echo$a; 6 ?>

Result

1 notice:undefined variable:a in C:\xampp\htdocs\test\p.php on line 5

Not be able to find, so there is no reference coming in, my PHP version is php/5.5.1

Learning Address:

Http://www.cnblogs.com/xia520pi/p/3697099.html

The difference between include and require in PHP

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.