The difference between include and require in PHP is described in detail.

Source: Internet
Author: User

The difference between include and require in PHP is described in detail.
1. Summary

The performance of the require () Statement is similar to that of include (), which includes and runs the specified file. The difference is that for the include () Statement, the file must be read and evaluated each time it is executed; For require (), the file is processed only once (in fact, replace the file content with the require () Statement ). This means that if the code may be executed multiple times, the use of require () is more efficient. On the other hand, if different files are read every time the code is executed, or there is a loop through a set of file iterations, The include () statement is used.

The use of require is as follows: require ("myfile. php"). This statement is usually placed at the beginning of the PHP script program. Before the PHP program is executed, it will first read the file introduced by the require () statement to make it part of the PHP script file. The use of include is the same as that of require, such as: include ("myfile. php"). This statement is generally placed in the Process Section of process control. When the include () Statement is read, the PHP script file reads the files it contains. In this way, you can simplify the process during program execution.

  • Incluce loaded when used
  • Require load at the beginning
  • _ Once suffix indicates that the loaded files are not loaded.

The PHP system has a pseudo-compilation process when loading the PHP program, which can speed up the program running. However, the CE documentation is still interpreted. An error occurred in the include file. The main program continues to execute. The require file has an error and the main program has stopped. Therefore, if the file error does not affect the system (such as interface files) use include; otherwise, use require.

The require () and include () statements are language structures and are not real functions. They can be the same as other language structures in php. For example, echo () can use echo ("AB ") format, you can also use echo "abc" to output the string abc. The require () and include () statements can also directly add parameters without parentheses.

The include_once () and require_once () Statements also include running the specified file during script execution. This behavior is similar to the include () Statement and require () statement, and the usage is the same. The only difference is that if the code in this file has been included, it will not be included again. These two statements should be used to ensure that the same file is only included once when it may be included more than once during script execution, to avoid function redefinition and variable re-assignment.

2. Details 2.1 Error

When the include file is introduced, if an error occurs, a prompt is displayed and the following code continues to run.

When a file is introduced by require, if an error occurs, a prompt is displayed and the following code is stopped.

Use example to speak, write two PHP files, named test-include.php and test-require.php, note that the same directory, do not exist a file named test-nothing.php.

test-include.php

<?php

include 'test-nothing.php';

echo 'abc';

?>

 

test-require.php

<?php

require 'test-nothing.php';

echo 'abc';

?>

Browse http: // localhost/test-include.php, because the test-nothing.php file is not found, we see the error message, at the same time, the error message below shows abc, you may see something similar to the following:

Warning: include(test-nothing.php) [function.include]: failed to open stream: No such file or directory in D:\www\test-include.php on line 2

Warning: include() [function.include]: Failed opening 'test-nothing.php' for inclusion (include_path='.;C:\php5\pear') in D:\www\test-include.php on line 2

abc

Browse http: // localhost/test-require.php, because the test-nothing.php file is not found, we see the error message, but the error message below does not show abc, you may see something similar to the following:

Warning: require(test-nothing.php) [function.require]: failed to open stream: No such file or directory in D:\www\test-require.php on line 2

Fatal error: require() [function.require]: Failed opening required 'test-nothing' (include_path='.;C:\php5\pear') in D:\www\test-require.php on line 2 

2.2 condition reference

The include () and require () functions are the same, but their usage is somewhat different. include () is a conditional include function, while require () is an unconditional include function.

For example, in the following example, if the variable $ somg is true, the file somefile. php will be included:

if($some){

  include 'somefile.php';

}

However, no matter what the value of $ some is, the following code will include the file somefile. php into the file:

if($something){

  require 'somefile.php';

}

The following examples fully illustrate the differences between the two functions:

$i = 1;

while ($i < 3) {

  require "somefile.$i.php";

  $i++;

}

From the code above, we can see that the program will include the same file in every loop. Obviously this is not what we want, we can see that this code will include different files in each loop. To complete this function, you can only use the include () function ():

$i = 1;

while ($i < 3) {

  include "somefile.$i.php";

  $i++;

}

2.3 file reference

Include () the file to be referenced during execution must be read and evaluated each time. require () the file to be referenced is only processed once during execution (the require () Statement replaces the file content to be referenced during actual execution) it can be seen that if there is code containing one of these commands and code that may be executed multiple times, the use of require () is more efficient, if different files are read each time code is executed or there is a loop through a set of file stacks, you can use include () to set variables for the file names you want to include, this variable is used when the parameter is include.

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.