Then speak PHP require (), include (), require_once () and include_once () their differences _php tutorials

Source: Internet
Author: User
This article describes the PHP four call external file function usage and differences, there is a need to know friends can refer to.

There are two ways to refer to a file: Require and include. Two ways to provide different use elasticity.

Require use methods such as require ("myrequirefile.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 ("myincludefile.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


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,
For example, if the variable $somg is true, the file somefile.php will be included:

The code is as follows Copy Code
if ($some) {include ' somefile.php ';}

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

The code is as follows Copy Code
if ($something) {require ' somefile.php ';}

The following example fully illustrates the difference between the two functions

The code is as follows Copy Code
$i = 1;
while ($i < 3) {require "somefile. $i. php"; $i + +;}

As 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 this code is expected in each loop,
Include the different files, if you want to complete this function, you can only use the function include ()

The code is as follows Copy Code
$i = 1;
while ($i < 3) {
Include "Somefile. $i. php"; $i + +;
}

3.require when using a relative path

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 '

The 4.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

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

The code is as follows Copy Code

conn.php
$dbh =mysql_connect (' localhost ', ' root ', ' 123456 ');
mysql_select_db (' db ', ' $dbh ');
?>
In the actual application, we call the file as:
Require ("conn.php") or include ("conn.php");
But if so:
filename.php
Require ("conn.php");
function Myfun ($par 1, $par 2)
{contains statements processed against the database}
.....
Myfun ($par 1, $par 2);
.....
Myfun ($p 1, $p 2);
?>


Summarize


Incluce load when used
Require is loaded at the beginning
The _once suffix indicates that the load is not loaded
The PHP system has a pseudo-compile process when loading PHP programs, which can make the program run faster. But Incluce's documentation is still interpreted to perform
An error occurred in the include file and the main program continues to execute
The Require file went wrong and the main program stopped.
So the included file error has little impact on the system (such as interface files) with include, otherwise with require
The following documents also apply to require (). These two structures are exactly the same in addition to how they handle failures. The include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing a page when you encounter a lost file, use require (). The include () is not the case and the script will continue to run. Also be sure to set the appropriate include_path.
The Require () function replaces itself with the contents of a given file, which occurs during the PHP engine's compile code, rather than during execution, and is not evaluated first, as in include (). The Require () function is used more in static elements, while the include () is more used in dynamic elements. Similar to Include_once (), require_once () will first check if the given code has been inserted, and if the code already exists, it will no longer be inserted.

http://www.bkjia.com/PHPjc/631631.html www.bkjia.com true http://www.bkjia.com/PHPjc/631631.html techarticle This article describes the PHP four call external file function usage and differences, there is a need to know friends can refer to. There are two ways to refer to a file: Require and include. Two ways of mentioning ...

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