Let's talk about the differences between require (), include (), require_once () and include_once () in php _ PHP Tutorial

Source: Internet
Author: User
Tags types of functions
Let's talk about the differences between require (), include (), require_once () and include_once () in php. This article introduces the usage and difference of four types of functions for calling external files in php. For more information, see. There are two methods to reference files: require and include. This article introduces the usage and difference of four methods for calling external file functions in php. For more information, see.

There are two methods to reference files: require and include. The two methods provide different elasticity.

The use of require is as follows: require ("MyRequireFile. 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 ("mydomaindefile. 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:

The code is as follows:
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:

The code is as follows:
If ($ something) {require 'somefile. php ';}

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

The code is as follows:
$ 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 ()

The code is as follows:
$ 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

The code is as follows:
$ 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 ().

The code is as follows:

Conn. php
$ Dbh = mysql_connect ('localhost', 'root', '123 ');
Mysql_select_db ('DB', '$ dbh ');
?>
In practical applications, we call files such:
Require ("conn. php") or include ("conn. php ");
However, if:
Filename. php
Require ("conn. php ");
Function myfun ($ par1, $ par2)
{Contains statements for database processing}
.....
Myfun ($ par1, $ par2 );
.....
Myfun ($ p1, $ p2 );
?>


Summary


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 error of the contained file does not affect the system much (such as the interface file), use include. Otherwise, use require.
The following documents also apply to require (). These two structures are identical except for how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when a file is lost, use require (). This is not the case with include (). The script will continue to run. Make sure that the appropriate include_path is set.
The require () function replaces itself with the content of a given file, which occurs during PHP engine code compilation rather than during execution, unlike include () the calculation is performed first. The require () function is more used in static elements, while the include () function is more used in dynamic elements. Similar to include_once (), require_once () first checks whether the given code has been inserted. if the code already exists, it is no longer inserted.

Bytes. There are two methods to reference files: require and include. Two methods...

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.