PHP include and require usage introduction _php tutorial

Source: Internet
Author: User
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.

The features of include () and require () are basically the same (included), but there are some differences in usage, including that include () is a conditional include function, and require () 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 ($a) {
Include ("a.php");
}


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.

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

The code is as follows Copy Code

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

In the case of error handling, the include statement is used, and if an error occurs, the program skips the include statement, although an error message is displayed but the program will continue to execute! But Requre will give you a fatal mistake.

Error

Use the example to speak, write two php files, name test1.php and test2.php, note the same directory, do not exist a name is test999.php file.

The code is as follows Copy Code

test.php
Include ("test999.php");
echo "ABC";
?>

test2.php
Require ("test999.php")
echo "ABC";
?>

Browse the first file, because we did not find the test999.php file, we see the error message, at the same time, the error message below the display of ABC, you can see a similar situation below:
Warning:include (test1aaa.php) [function.include]: failed to open stream:no such file or directory in d:websitetest.php o N Line 2

Warning:include () [function.include]: Failed opening ' test1aaa.php ' for inclusion (include_path= '.; C:php5pear ') in d:websitetest.php on line 2
Abc

Browse the second file, because we did not find the test999.php file, we see the error message, but, the error message is not shown below the ABC, you can see the situation is similar to the following:
Warning:require (test1aaa.php) [function.require]: failed to open stream:no such file or directory in d:websitetest.php o N Line 2

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

http://www.bkjia.com/PHPjc/632228.html www.bkjia.com true http://www.bkjia.com/PHPjc/632228.html techarticle 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 in the ...

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