The difference between include and require in PHP

Source: Internet
Author: User

The following is transferred from: 51089785

There are too many differences between the include and require in PHP. In fact, is not can not withstand the verification. Feel free to find one of the following:


Also vowed to explain:include () is a conditional inclusion function, and require () is an unconditional inclusion function.

But this is a long time ago, and now the Php,require and include do have a difference, the author tested the results of more than 5.3 version of the test result is as follows:

These two structures are found to be identical except in performance and in how they handle containment failures.

1. In terms of performance

The test found that when the require and include in the performance of a small difference, I think this can not be considered (perhaps I test the sample is not enough)

eg

1<pre name= "Code"class= "PHP" >$startTime= Time();2 $startMemoryUsed=Memory_get_usage (); 3 require"A1.inc";4 $endMemoryUsed=memory_get_usage ();5 $endTime= Time();6 $usedMemory=($endMemoryUsed-$startMemoryUsed)/1024; 7 Print_r($usedMemory);//1.61718758 Echo' </br> ';9 Print_r($endTime-$startTime);Ten /*$startTime =time (); One $startMemoryUsed =memory_get_usage ();  A include "A1.inc"; - $endMemoryUsed =memory_get_usage ();  - $endTime =time (); the $usedMemory = ($endMemoryUsed-$startMemoryUsed)/1024; - Print_r ($usedMemory);//1.6171875 - Print_r ($endTime-$startTime);*/
View Code

2. Include error handling

Include () generates a warning and continues execution when it contains a failure, while require () causes a fatal error. In other words, if you want to stop processing a page when you encounter a lost file, use require (), or include ().

    1. Include "A9.inc"; <span style= "font-family:arial, Helvetica, Sans-serif;" >a9.inc does not exist will produce a warning </span>
    2. Require "A10.inc"; A10.inc does not exist will produce a fatal error

Supplemental:Include,require contains scopes

File A2.inc

1 $name= "Lidquan";2 classteacher{3      Public functionShow () {4         Echo' My job is teacher</br> ';5     }6 }7 functionShowName () {8     Echo' The method inside the A2.inc ';9}
View Code


File index.php

1 functionA () {2     require"A2.inc";3     $teacher=NewTeacher ();4     $teacher-Show ();5 }6  7A ();//Normal Call8  9ShowName ();//Normal CallTen   One Var_dump($name);//hint $name not defined
View Code

The results are as follows:


A summary of scope issues after the include file is summarized below:

1. All functions and classes defined in the contained file are included and have global scope in the containing file

2. The scope of the variable that contains the file changes as the containing position changes. For example, if it is included in a function, the variable containing the file is a local variable

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.