What is the difference between include and require in PHP? Look at the examples here, you know.

Source: Internet
Author: User
Tags include require variable
The results of the include| difference include.php3 are:
This is the value of a variable in the Inc1.inc file!
This is the value of a variable in the Inc2.inc file!
The $int variable value in the Inc1.inc file is 1!

The results of the REQUIRE.PHP3 operation are:
This is the value of a variable in the Inc1.inc file!
The $int variable value in the Inc1.inc file is 2!

You can see that in the Require.php3, the $int has changed to 2, That is, the statement in Inc1.inc is executed 2 times, so it seems that the require statement is only interpreted once in the loop, and the location of the require statement is replaced and run with the Require file content, and the include statement is run every time in the loop.

[Sonymusic] Added:
Require is only executed once, no, that's not appropriate. It should be said that require is the first alternative to the contents of the specified file to be replaced, and then run, so it does not know you set up a for loop. The include statement, when it is executed, and what the contents of the specified file are brought in, continues to execute.

INCLUDE.PHP3:
<body>
?
For ($i =1 $i <=2; $i + +) {
Include ("Inc$i.inc");
}
echo $var 1;
echo $var 2;
The $int variable value in the Echo ' Inc1.inc file is '. $int. "! <br> ";
?>
</body>

REQUIRE.PHP3:
<body>
?
For ($i =1 $i <=2; $i + +) {
Require ("Inc$i.inc");
}
echo $var 1;
echo $var 2;
The $int variable value in the Echo ' Inc1.inc file is '. $int. "! <br> ";
?>
</body>

Inc1.inc:
?
$var 1 = "This is the value of a variable in the Inc1.inc file!" <br> ";
if (Isset ($int)) {
$int + +;
}
else{
$int = 1;
}
?>

Inc2.inc:
?
$var 2 = "This is the value of a variable in the Inc2.inc file!" <br> ";
?>



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.