The old topic has to be asked again. in the include and require manuals, there is only one difference between include and require, that is, different levels of errors are generated when an error occurs. However, the difference between the two on the Internet is far more than that. I am very confused. The following are some of my views and questions I have extracted from the internet. I have to ask again include and require.
In the manual, there is only one difference between include and require, that is, different levels of errors are generated when an error occurs.
However, the difference between the two on the Internet is far more than that, which makes me very confused.
The following are some of my views and questions on the Internet.
Bytes --------------------------------------------------------------------------------------------------------
Reference
When you use the require statement to call a file, as long as the program is executed, the external file will be called immediately. when you use the include statement to call an external file, only when the program executes this statement, to call an external file.
What is "called as soon as the program is executed "?
//aa.php
echo 'aa';
require './bb.php';
I think so for the time being, because I don't know how to compile it. Please advise
Require: when aa. php is executed, the aa. php and bb. php are compiled together and then executed;
Include: first set aa. php compilation-> execution, found include, and then put bb. php compilation-> execute bb. php, return to continue executing aa. php code.
Bytes --------------------------------------------------------------------------------------------------------
Reference
The file contained by require () cannot contain a control structure and cannot use statements such as return. Using the return statement in files contained in require () produces processing errors.
This is very hard to understand. Aa. php is still the code above. below is bb. php, which is well executed.
//bb.php
if(true){
echo 'bb';
}
return 'abc';
Bytes --------------------------------------------------------------------------------------------------------
Reference
Require () cannot contain different files in the loop body based on different conditions. The require () statement will only replace itself with the content in the file it contains when it is executed for the first time. when it is executed again, it can only execute the statements contained for the first time. However, the include () statement can contain different files in the loop body.
The article provides an example and draws a conclusion:
Reference
From the code above, we can see that the program will include the same file in every loop. obviously this is not what we want, we can see that this code will include different files in each loop. to complete this function, you can only use the include () function ()
//aa.php
echo 'aa';
$i=1;
while($i<3){
require './test' . $i . '.php';
$i++;
}
However, I can introduce both test1.php and test2.php into the test.
echo 'test1';
echo 'test2';
Output: aatest1test2
Bytes --------------------------------------------------------------------------------------------------------------------------
Reference
Include () is a conditional include function, while require () is an unconditional include function. This means that require is used. in a condition control statement, even if the condition is false, it will be introduced. However, the imported file does not play any visible role, but will only increase the burden on the program.
//aa.php
if(false){
require './bb.php';
}
Is that true? How can I prove that bb. php has been introduced?
Bytes ------------------------------------------------------------------------------------------------------------------------
In addition, there is another question about error reporting, which is described in the manual.
//aa.php
include 'nofile.php';
//require 'nofile.php';
The following error is reported:
Reference
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
Warning: include () [function. include]: Failed opening 'nofile. php 'for declaration (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 error does the first warning report? Both are warning
The second one seems to be an error that cannot be found in include_path?
------ Solution ----------------------
All instructions 1. latest manual shall prevail
The description of include and require on the internet is mostly based on php4.
In the php5 era, there is only the difference between different levels of errors generated when errors occur.
------ Solution ----------------------
Updating IT knowledge is too fast. refer to the official manual. you can also read the PHP source code to see how IT is handled. that is the most correct.