Difference between include and require in PHP instance analysis and require instance analysis

Source: Internet
Author: User

Difference between include and require in PHP instance analysis and require instance analysis

Edit the command. php file first

echo 'hello'.PHP_EOL;

Edit the console. php file.

for($i=1;$i<=3;++$i){require 'command1.php';}

I originally wanted to include and execute this echo. I did not expect to write the wrong file name. If it is require, this error will be reported:

Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4Fatal error: require(): Failed opening required 'command1.php' (include_path='.') in 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 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';}

The following error is reported:

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 file 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='.') 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='.') 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='.') in console.php on line 4

If require_once or include_once is used, the loop is executed only once if the inclusion path is correct.

Summary:

If require is used, if the file is not included successfully, a fatal error is reported, and the entire program is aborted.

Use include. If the file is not successfully included, a normal warning will be reported and subsequent code will still be executed.

Use require_once if your Web application uses the MVC design method that is highly dependent on files.

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.