First prepare a file to include a.php, the file content is as follows
$a = 1;?>
Test require_once, the content is as follows
<? PHP for ($i$i<=10; $i++ ) {require_once(' a.php ') ; Echo $a+ +;}
The output is as follows, because only the first containment is valid, and subsequent inclusions are not valid.
12345678910
Test require, the content is as follows
<? PHP for ($i$i<=10; $i++ ) {require(' a.php ') ; Echo $a+ +;}
The output is as follows, each time to execute the inclusion, so the output is 1
1111111111
Change require and require_once to include and include_once perform the same effect.
2. The difference between include and require is that the error is not handled in the same way
Require
<? PHP require (' b.php '); Echo "Contiue code \ n";
The execution result is as follows, continue code this sentence does not have the output, indicates the require contains the file if encounters the error, the later code does not carry out the
PHP Warning: requirefile or directory in/home/lxy/htdocs/test.php on line 2php Fatal error
: require(): Failed opening required ' b.php ' (include_path= '.: ') in/home/lxy/htdocs/test.php on line 2
Include
<? PHP include (' b.php '); Echo "Contiue code \ n";
The execution results are as follows, continue code outputs the
PHP Warning: includefile or directory in/home/lxy/htdocs/test.php on line 3contiue code PHP Wa Rning: include for inclusion (include_path= '.: ') in/home/lxy/htdocs/test.php on line 3
The difference between include and require