There are too many differences between the include and require in PHP. But is it true that today we have a concrete example of a simple analysis to verify that
Edit the command.php file first
echo ' Hello '. Php_eol;
Then edit the console.php file
for ($i =1; $i <=3;++ $i) {require ' command1.php ';}
Originally wanted to include and execute this echo, did not expect to write the wrong file name, if it is require, will report such an error:
Warning:require (command1.php): Failed to open stream:no such file or directory in console.php on line 4Fatal error:requ IRE (): Failed opening required ' command1.php ' (include_path= '. ') console.php on line 4PHP Warning:require (command1.php ): Failed to open stream:no such file or directory in console.php on line 4PHP Fatal Error:require (): Failed opening req Uired ' command1.php ' (include_path= ') in console.php on line 4
If you change the require to include
for ($i =1; $i <=3;++ $i) {include ' command1.php ';}
will report such an error:
Warning:include (command1.php): Failed to open stream:no such file or directory in console.php on line 4warning:include ( ): Failed opening ' command1.php ' for inclusion (include_path= '. ") in console.php on line 4warning:include (command1.php): Failed to open stream:no such file or directory in console.php on line 4warning:include (): Failed opening ' command1.php ' For inclusion (include_path= '. ") in console.php on line 4warning:include (command1.php): Failed to open stream:no such F Ile or directory in console.php on line 4warning:include (): Failed opening ' command1.php ' for inclusion (include_path= '. ') ) in console.php on line 4PHP warning:include (command1.php): Failed to open stream:no such file or directory in console. PHP on line 4PHP warning:include (): Failed opening ' command1.php ' for inclusion (include_path= '. ') 4PHP Warning:include (command1.php): Failed to open stream:no such file or directory in console.php on line 4PHP Warning : Include (): Failed Opening ' command1.php ' for inclusion (include_path= '. ") in console.php on line 4PHP warning:include (command1.php): Failed to Open Stream:no such file or directory in console.php on line 4PHP warning:include (): Failed opening ' command1.php ' for I Nclusion (include_path= '. ') in console.php on line 4
If you use require_once or include_once, the loop executes only once if the containing path is correct.
Summarize:
With require, if the file does not contain a success, a fatal error is reported, and the entire program is aborted.
With include, if the file does not contain a success, a normal warning is reported, and then the code is still executed.
If your Web program uses MVC as a design method that contains strong dependencies on files, use require_once.