The difference between PHP function Import and file inclusion: include () and require () statements

Source: Internet
Author: User
Tags pear
To use a function defined in a file in a PHP script, you need to use the include (), include_once (), require (), and require_once (), a function library file is loaded into the script program. In the previous section we talked about how to use a custom function library in PHP, which is a specific discussion of the differences between require () and include ().

PHP provides two ways to include external files: require () and include () are common methods in server-side references (server Side includes).

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. Require contains the file when it is absolutely included, if the contained file does not exist then it will produce a fatal error (fatal error), after the error occurs, the following script will not be executed, in addition, Because the Require statement is equivalent to completely copying the contents of another source file into this file, it is generally placed in the starting position of the source file, referencing the public function file and the public class file that you want to use.

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 include file is relative, and if the included file does not exist, a warning warning is generated, but the following script continues to execute, and the include file is generally written in program execution.

The difference between include () and require ()

The difference between include () and require () this is believed to be the most basic problem in PHP, the use of the two is exactly the same, does not have to be placed at the front of which one in the middle. Their most fundamental difference is that they are not handled in the same way.

Require (): If the file does not exist, a fatal error is reported. The script stops executing

Include (): If the file does not exist, it will give a warning, but the script will continue to execute

PHP's require () performance is similar to include (). The difference is that, for the include (), the file is read and evaluated every time the include () is executed, and for require (), the file is processed only once (in fact, the file contents replace the Require () statement). This means that if you have code that contains one of these directives and code that might execute multiple times, using require () is a high efficiency. On the other hand, if you read a different file each time you execute the code, or you have a loop that iterates through a set of files, you use include () because you can set a variable for the file name you want to include, and use that variable when the parameter is include ().

Some other links and differences in the supplement

1. Include has a return value, and require does not.

2. Include () includes and runs the specified file when processing fails the include () generates a warning that the imported program code will be executed and that the program will execute with the same range of variables as the location of the call to the include () statement in the source file. You can import static pages from the same server.

3. The role of Include_once () and the include () are almost identical

The only difference is that include_once () will first check if the file to be imported has been imported elsewhere in the program, and if it does not repeat the import again (this feature is sometimes important, for example, to import a declaration of some functions that you define yourself, If you import this file repeatedly in the same program, an error message will occur on the second import because PHP does not allow the same name function to be repeated for the second time.

4. require () will read the contents of the target file and replace itself with these read-in content require () causes a fatal error when processing fails.

5. Like include_once (), require_once () will first check that the contents of the target file have been imported before, and if so, it will not re-import the same content again.

6. Require is an unconditional inclusion, that is, if a process joins require, the require will be executed first, whether or not the condition is established.

The following example illustrates the difference between the two functions:

Write two php files, named test-include.php and test-require.php, note the same directory, do not exist a name is test1.php file.

Test-include.php<?phpinclude ' test1.php '; Echo ' abc ';? >

Run the above code, because did not find the test1.php file, we see the error message, at the same time, the error message below the display of ABC, you see may be similar to the situation below:

Warning:include (test1.php) [function.include]: failed to open stream:no such file or directory in D:\www\test-include.ph P on line 3warning:include () [function.include]: Failed opening ' test1.php ' for inclusion (include_path= '.; C:\php5\pear ') in D:\www\test-include.php on line 3abc
Test-require.php<?phprequire ' test1.php '; Echo ' abc ';? >

Run the above code, because we did not find the test1.php file, we see the error message, but, the error message is not shown below the ABC, you can see a similar situation below:

Warning:require (test1.php) [function.require]: failed to open stream:no such file or directory in D:\www\test-require.ph P on line 3Fatal error:require () [function.require]: Failed opening required ' test1 ' (include_path= '); C:\php5\pear ') in D:\www\test-require.php on line 3

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. Video Tutorial: Function Import and file inclusion: Use of include and require

3. PHP real-Combat video tutorial

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.