I have to ask the include and require again.

Source: Internet
Author: User
Tags pear php source code
Old topic, have to ask again include and require
There is only one difference between include and require in the manual, which is the different levels of error that occur when an error occurs.
But the difference between the two on the internet is far more than that, making me very confused.
Here are some of the ideas and questions I've extracted from the Internet.
--------------------------------------------------------------------------------------------------------
When invoking a file using the Require statement, the external file is called immediately as soon as the program executes, and when an external file is invoked through the include statement, the external file is called only when the program executes to the statement.

What is "call as soon as the program executes"?
 
  aa.php
echo ' AA ';
Require './bb.php ';

I think so for the time being, because I don't know anything about compiling or something. Please advise
Require situation: When executing aa.php, compile the aa.php and bb.php together, then execute;
Include: first put aa.php compile-and-execute, found that there is an include, and then the bb.php compile-and-execute bb.php, completed after the return to continue execution aa.php the following code.
--------------------------------------------------------------------------------------------------------
Reference
require () contains a file that cannot contain a control structure and cannot use a statement such as return. Using the return statement in a file contained in require () will result in processing errors

This is a very confusing point. aa.php or the above code, the following is bb.php, executed well
 
  bb.php
if (true) {
Echo ' BB ';
}
Return ' ABC ';

--------------------------------------------------------------------------------------------------------
Reference
require () cannot contain different files in the loop body depending on the condition. The Require () statement will only invoke the contents of the file it contains in the first execution and replace the statement itself, and when executed again, it can only execute the statement that was contained for the first time. However, the include () statement can contain different files in the loop body.

The article cited an example and concludes that:
The reference
can be seen from the above code, each time the loop, the program will be the same file included, it is clear that this is not what we want, we can see that the code wants to be in each loop, the different files included, if you want to complete this function, Use only function include ()

 
  aa.php
echo ' AA ';
$i = 1;
while ($i <3) {
Require './test '. $i. '. php ';
$i + +;
}

But I'm testing to bring both test1.php and test2.php in.
 
  Echo ' test1 ';

 
  Echo ' Test2 ';

Output: Aatest1test2
--------------------------------------------------------------------------------------------------------------- -----------
The reference
include () is a conditional include function, and require () is an unconditional include function. This means that using require, in conditional control statements, even if the condition is false, it is introduced. It is just that the files that are introduced do not play any visible role and only add to the burden of the program.

 
  aa.php
if (false) {
Require './bb.php ';
}

Is that so? How can it be proved that bb.php has been introduced?
--------------------------------------------------------------------------------------------------------------- ---------
In addition, there is a question about the error, the manual says

 
  aa.php
Include ' nofile.php ';
Require ' nofile.php ';

The error is as follows:
References
warning:include (nofile.php) [function.include]: failed to open stream:no such file or directory in D:\www\Apache2 .2\htdocs\02php\index.php on line 3

Include when
Warning:include () [function.include]: Failed opening ' nofile.php ' for inclusion (include_path= '.; C:\php\pear ') in D:\www\Apache2.2\htdocs\02PHP\index.php on line 3

When require
Fatal Error:require () [function.require]: Failed opening required ' nofile.php ' (include_path= '); C:\php\pear ') in D:\www\Apache2.2\htdocs\02PHP\index.php
on line 3
What kind of mistake did the first warning report? Two of them are warning.
The second one looks like you can't find it in include_path.
------to solve the idea----------------------
All descriptions of the latest manual are subject to

The online descriptions of include and require are mostly based on PHP4
By the time of the PHP5, there was only the difference between the different levels of error that occurred at the time of the error.
------to solve the idea----------------------
It knowledge of the update is too fast, all the official manual is the standard, you can also read the PHP source code, to see how to deal with, that is the most correct.
  • 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.