What is the difference between the include () function and the Require () function in PHP?

Source: Internet
Author: User
This article brings you to what is the difference between the include () function and the Require () function in PHP? There is a certain reference value, the need for friends can refer to, I hope to help you.

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 exactly the same 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)

<pre name= "code" class= "PHP" > $startTime =time (); $startMemoryUsed =memory_get_usage ();  Require "A1.inc", $endMemoryUsed =memory_get_usage (), $endTime =time (), $usedMemory = ($endMemoryUsed-$startMemoryUsed )/1024; Print_r ($usedMemory);//1.6171875echo ' </br> ';p rint_r ($endTime-$startTime);/* $startTime =time (); $ Startmemoryused=memory_get_usage ();  Include "A1.inc"; $endMemoryUsed =memory_get_usage ();  $endTime =time (); $usedMemory = ($endMemoryUsed-$startMemoryUsed)/1024; Print_r ($usedMemory);//1.6171875print_r ($endTime-$startTime); */

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 ().

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

Supplemental: Include,require contains scopes

File A2.inc

$name = "Lidquan"; class teacher{public    function Show () {        echo ' My job is teacher</br> ';    }} function ShowName () {    echo ' a2.inc inside method ';}

File index.php

function A () {    require "a2.inc";    $teacher =new Teacher ();    $teacher->show ();} A ();//Normal call ShowName ();//Normal call Var_dump ($name);//hint $name not defined

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 containing 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

Related Article

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.