The difference between require (), include (), require_once (), and include_once () _php Tutorial

Source: Internet
Author: User
There are two ways to refer to a file: Require and include.
Require use methods such as 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 introduces the file, if you encounter errors, will give a hint, and continue to run the code below
Require introduce a file, if you encounter 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

Include () is functionally the same as require (), with some differences in usage, include () is a conditional include function, and require () is an unconditional include function,
For example, if the variable $somg is true, Will contain the file somefile.php:
if ($some) {

include ' somefile.php ';
}
But whatever value $some takes, the following code will include the file somefile.php in 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 + +;
} The
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,
to include different files, if you want to complete this function, you can only use the function include ()
$i = 1;
while ($i < 3) {
include "somefile. $i. php";
$i + +;
}
3.require with relative path
When a refers to B, and B refers to other file C, the path of C is relative to a path, and not the '
4.require_once () relative to B. Statement to include and run 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";
}
The files that are referenced when the include () are executed are read and evaluated every time
Require () executes a file that needs to be referenced only once (the Require () statement is actually replaced with the file content to be referenced when executing)
You can see that if you have code that contains one of these directives and code that might execute multiple times, using require () is a high efficiency,
If you read different files each time you execute the code or have loops that pass through a set of file iterations, use the Include (),
You can set a variable for the file name you want to include, and use this variable when the parameter is include ()


Excerpted from 〃styleひぐ

http://www.bkjia.com/PHPjc/478295.html www.bkjia.com true http://www.bkjia.com/PHPjc/478295.html techarticle There are two ways to refer to a file: Require and include. Require use methods such as require (file.php);. This function is usually placed at the front of the PHP program, before the PHP program is executed ...

  • 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.