Php include and require usage

Source: Internet
Author: User

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.

 

The include () and require () functions are basically the same (include), but there are also some differences in usage. include () is a conditional include function, while require () it is an unconditional include function. For example, in the following code, if the variable $ a is true, it will contain the file a. php:

The Code is as follows: Copy code

If ($ ){
Include ("a. php ");
}


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.

 

The require () is different from include (). No matter what the value of $ a is, the following code will include file a. php into the file:

The Code is as follows: Copy code

If ($ ){
Require ("a. php ");
}

For error handling, use the include statement. If an include error occurs, the program skips the include statement. Although the error message is displayed, the program continues to execute! But requre will give you a fatal error.

Error

 

Use an example to write two PHP files named test1.php and test2.php. Note that there is no file named test999.php in the same directory.

The Code is as follows: Copy code

Test. php
<? PHP
Include ("test999.php ");
Echo "abc ";
?>

Test2.php
<? PHP
Require ("test999.php ")
Echo "abc ";
?>

Browse the first file. Because the test999.php file is not found, we can see the error message. At the same time, abc is displayed under the error message, which may be similar to the following:
Warning: include (test1aaa. php) [function. include]: failed to open stream: No such file or directory in D: WebSitetest. php on line 2

Warning: include () [function. include]: Failed opening 'test1aaa. php' for future Sion (include_path = '.; C: php5pear') in D: WebSitetest. php on line 2
Abc

Browsing the second file, because the test999.php file is not found, we can see the error message. However, the error message below does not show abc. What you see may be similar to the following:
Warning: require (test1aaa. php) [function. require]: failed to open stream: No such file or directory in D: WebSitetest. php on line 2

Fatal error: require () [function. require]: Failed opening required 'test1aaa. php' (include_path = '.; C: php5pear') in D: WebSitetest. php on line 2

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.