Home > Article > Backend Development > Detailed explanation of the difference between include and require in PHP
There is too much information on the Internet about the difference between include and require in PHP. But is this really the case? Today we will briefly analyze and verify it through a specific example
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'; }
I originally wanted to include and execute this echo, but I didn’t expect that I wrote the wrong file name. If it is require, it will be reported Such an error:
Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4 Fatal error: require(): Failed opening required 'command1.php' (include_path='.') in console.php on line 4 PHP Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Fatal error: require(): Failed opening required 'command1.php' (include_path='.') in console.php on line 4
If you change require to include
for($i=1;$i<=3;++$i){ include 'command1.php'; }
will be reported An error like this occurs:
Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4
If you use require_once or include_once, as long as the include path is correct, the loop will only be executed once.
Summary:
Using require, if the file is not included successfully, a fatal error will be reported and the entire program will be terminated.
Using include, if the file is not included successfully, a normal warning will be reported, and the subsequent code will still be executed.
If your web program uses MVC, a design method that contains strong dependencies on files, please use require_once.
Detailed explanation of require, include, use distinction in php
Explanation on PHP Include files
PHP basic learning includes require, include
The above is the detailed content of Detailed explanation of the difference between include and require in PHP. For more information, please follow other related articles on the PHP Chinese website!