Using the include and require to differentiate instance analysis

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.