PHP include () and require ()

Source: Internet
Author: User

There are two ways to refer to a file:require and include.

The use of require is as follows require("file.php"); . This function is usually placed at the front of the PHP program, and before the PHP program executes, it is read into the file specified by require to make it a part of the PHP program's Web page. Commonly used functions, you can also use this method to introduce it into the Web page.

include usage methods such as include("file.php"); . This function is usually placed in the processing part of the process control. The PHP Program page reads the include file before it is read in. This way, you can simplify the process when the program executes.

The _once suffix indicates that the load is not loaded

1. error include when introducing the file, if you encounter errors, will give a hint, and continue to run the code below require the introduction of the file, if encountered errors, will give a hint, and stop running the code below

Note: Before PHP 4.3.5, a syntax error in the include file does not cause the program to stop, but from this version.

2. Conditional References

The Include () function is the same as require (), there are some differences in usage, include () is a conditional include function, and require () is an unconditional include function, such as the following example, if the variable $somg is true, Will contain the file somefile.php:

if ($some) {<br>  include ' somefile.php ';}

However, regardless of the $some value, the following code will include the file somefile.php into the file:

if ($something) {  require ' somefile.php ';}

The following example fully illustrates the difference between the two functions

$i = 1;  while ($i < 3) {  require "somefile.  $i. php ";   $i+ +;}

Can be seen from the above code, each time the loop, the program will be the same file included, it is clear that this is not what we want, we can see that the code wants to be in each loop, the different files are included, if you want to complete this function, you can only use the function include ()

$i = 1;<br> while ($i < 3) {  include ' Somefile.  $i. php ";   $i+ +;}

3 . require when using relative paths

When a refers to B, and B refers to other file C, the path to C is relative to the path of a, and not to the B '

4.the require_once () statement includes and runs the specified file during script execution. This behavior is similar to the Require () statement, except that if the code in the file is already 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, except that if the code in the file is already included, it will not be included again. As the name of this statement implies, it is included only once.

5.. File Reference Method

Include has a return value, and require does not

$login include (' Test. php '); if (! Empty ($login)) {  echo  "file contains success";} Else {  echo  "file contains failed";}

Include () files that need to be referenced each time they are read and evaluated, require () will need to refer to the file to be processed only once (in fact, the file content to be referenced at the time of execution replaces the require () statement) It can be seen that if you have code that contains one of these directives and code that might execute multiple times, using require () is more efficient, and if you are reading a different file each time you execute the code or have a loop that passes through a set of file iterations, you can set the variable to the file name you want to include when the parameter is Include () when using this variable

Summarize
The difference between require and include is that when an error occurs, one is an error (the wrong one) and the other is a warning (warning).

PHP include () and require ()

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.