Example analysis of the difference between using include and require

怪我咯
Release: 2023-03-12 20:28:01
Original
1053 people have browsed it

There are too many comments 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

First edit the command.php file

echo 'hello'.PHP_EOL;
Copy after login

Then edit the console.php file

for($i=1;$i<=3;++$i){
	require &#39;command1.php&#39;;
}
Copy after login

Originally I wanted to include and execute this echo, but I didn’t expect that I wrote the wrong file name. If it is require, an error like this will be reported:

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 &#39;command1.php&#39; (include_path=&#39;.&#39;) 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 &#39;command1.php&#39; (include_path=&#39;.&#39;) in console.php on line 4
Copy after login

If you change require to include

for($i=1;$i<=3;++$i){
	include &#39;command1.php&#39;;
}
Copy after login

An error like this will be reported:

Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4

Warning: include(): Failed opening &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) 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 &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) 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 &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) 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 &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) 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 &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) 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 &#39;command1.php&#39; for inclusion (include_path=&#39;.&#39;) in console.php on line 4
Copy after login

If you use require_once or include_once, as long as the include path is correct, then 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.

The above is the detailed content of Example analysis of the difference between using include and require. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template