PHP include and require use method difference detailed _php skill

Source: Internet
Author: User
Tags pear

In PHP, include () and require () have the same functionality, include (include_once) and require (require_once) are to read the included file code into the specified location, But there is a difference between the two: (Include () is a conditional include function, and require () is an unconditional inclusion function)

1, the use of different ways

(1) Require use methods such as require ("requirefile.php");. This function is usually placed at the top of the PHP program, before the PHP program is executed, it will read into the file introduced by require, making it a part of the PHP Program Web page. A commonly used function can also be introduced into a Web page in this way. The introduction is unconditional and occurs before the program executes, regardless of whether or not the condition is set up (may not be performed).
(2) Include use method such as include ("includefile.php");. This function is typically placed in the processing section of the Process Control. The PHP Program page reads the included file when it is read. This way, you can simplify the process of executing your program. The introduction is conditional, occurs when the program executes, only when the condition is set up (which simplifies compiling the generated code).

For example, in the following example, if the variable $somgthing is true, the file Somefile will be included:

Copy Code code as follows:

if ($something) {
Include ("Somefile");
}

But no matter what value the $something takes, the following code will include the file Somefile in the file:
Copy Code code as follows:

if ($something) {
Require ("Somefile");
}

The following interesting example fully illustrates the difference between the two functions.
Copy Code code as follows:

$i = 1;
while ($i < 3) {
Require ("Somefile. $i");
$i + +;
}

In this code, every time a loop is made, the program will include the same file. Obviously this is not the intention of the programmer, and from the code we can see that the code wants to include different files in each loop. If you want to complete this function, you must turn to the function include ():
Copy Code code as follows:

$i = 1;
while ($i < 3) {
Include ("Somefile. $i");
$i + +;
}

2. The implementation of the Times wrong way different

Include and require the difference: include when the file, if encountered errors, will give a hint, and continue to run the following code, require introduced the file, if encountered errors, will give a hint, and stop running the code below. For example, the following example:


Write two php files, named test1.php and test2.php, note the same directory, do not exist a name is test3.php file.

test1.php

Copy Code code as follows:

? Php
Include ("test3.php");
echo "ABC";
?>

test2.php
Copy Code code as follows:

? Php
Require ("test3.php")
echo "ABC";
?>

Browse the first file, because we did not find the test999.php file, we saw the error message, at the same time, the error message below shows the ABC, you can see is similar to the following situation:
Warning:include (test3.php) [function.include]: failed to open stream:no such file or directory into D:\WebSite\test.php on Line 2

Warning:include () [function.include]: Failed opening ' test3.php ' for inclusion (include_path= '.; C:\php5\pear ') in D:\WebSite\test.php on line 2
ABC (the following was carried out)

Browsing the second file, because we did not find the test3.php file, we saw the error message, but the error message does not show the bottom of the ABC, you can see is similar to the following situation:
Warning:require (test3.php) [function.require]: failed to open stream:no such file or directory in D:\WebSite\test2.php o N Line 2

Fatal Error:require () [function.require]: Failed opening required ' test3.php ' (include_path= '.; C:\php5\pear ') in D:\WebSite\test.php on line 2

The following is not executed and ends directly
In short, an include is invoked when executed, is a process behavior, conditional, and require is a preset behavior, unconditional.

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.