Difference between require (), include (), require_once () and include_once ()

Source: Internet
Author: User
There are two methods to reference files: require and include. The use of require is as follows: require (& quot; file. php & quot ;);. This function is usually placed at the beginning of the PHP program. before the PHP program is executed, it will first read the file specified by require to make it into... Sy

There are two methods to reference files: require and include.
The use of require is as follows: require ("file. php ");. This function is usually placed at the beginning of the PHP program. before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage. This method can also be used to introduce common functions into webpages.
Include usage methods such as include ("file. php ");. This function is generally placed in the process of process control. The PHP program webpage reads the include file. In this way, you can simplify the process during program execution.
_ Once suffix indicates that the loaded files are not loaded.
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.
Note: Before PHP 4.3.5, the program will not be stopped due to syntax errors in the included files, but will be executed after this version.
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 ++;
}
We can see from the code above that the program will include the same file in every loop. obviously this is not what we want, we can see that this code is expected,
Include different files. to complete this function, you can only use the include () function ()
$ I = 1;
While ($ I <3 ){
Include "somefile. $ I. php ";
$ I ++;
}
3. When require uses relative paths
When A references B and B references other files C, the path of C is relative to A, not B'
4. the require_once () statement includes and runs the specified file during script execution. This behavior is similar to the require () statement. The only difference is that if the code in the file has been included, it will not be included again.
The include_once () statement includes and runs the specified file during script execution. This behavior is similar to the include () statement. The only difference is that if the code in the file has been included, it will not be included again. As the statement name implies, it will only be included once.

5. file reference
Include has a return value, while require does not
$ Login = include ('test. php ');
If (! Empty ($ login )){
Echo "file inclusion successful ";
} Else {
Echo "file inclusion failed ";
}
When the include () operation is performed, the file to be referenced must be read and evaluated each time,
Require () only processes the file to be referenced once during execution (the file content to be referenced in actual execution replaces the require () statement)
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 the code is executed, or there is a loop through a set of file stacks, use include (),
You can set a variable for the file name you want to include. this variable is used when the parameter is include ().

 
From neural Style neural networks

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.