Difference between require and require_once in php _ PHP Tutorial

Source: Internet
Author: User
Description of the difference between require and require_once in php. Include () and require (): The statement includes and runs the specified file. Include () generates a warning and require () causes a fatal error. In other words, if you want to encounter missing files include () and require (): The statement includes and runs the specified file.
Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when a file is lost, use require (). This is not the case with include (). The script will continue to run.

The require_once () statement includes and runs the specified file during script execution. This behavior is similar to the require () statement. The only difference is that if the code in the file has been included, it will not be included again.
The include_once () statement includes and runs the specified file during script execution. This behavior is similar to the include () statement. The only difference is that if the code in the file has been included, it will not be included again. As the statement name implies, it will only be included once.

1. the include () function reads the specified file and runs the program.

Example: include ('/home/me/myfile ');

The program code in the imported file will be executed, and these programs will have the same variable scope as the variable scope called to the include () function in the source file during execution ). You can import static files on the same server, or even import files on other servers by combining the include () and fopen () functions.

2. the role of the include_once () function is almost the same as that of the include () function.

The only difference is that the include_once () function first checks whether the file to be imported has been imported elsewhere in the program, if yes, the file will not be imported again (this function is sometimes very important. for example, some functions you have defined in the file to be imported, if the file is repeatedly imported in the same program, an error message will occur during the second import, because PHP does not allow the function with the same name to be repeated for the second time ).

3. The require () function will read the content of the target file and replace itself with the read content.

This read-and substitute action occurs when the PHP engine compiles your program code, rather than when the PHP engine starts to execute compiled program code (the PHP 3.0 engine works by compiling a line to execute a line, but it changes to PHP 4.0, PHP 4.0 is to compile the entire program code first, and then execute the compiled program code once. during the compilation process, no program code will be executed ). Require () is usually used to import static content, while include () is suitable for importing dynamic program code.

4. like the include_once () function, the require_once () function first checks whether the content of the target file has been imported before. If yes, the same content will not be imported again.

Http://www.bkjia.com/PHPjc/736849.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/736849.htmlTechArticleinclude () and require (): The statement includes and runs the specified file. Include () generates a warning and require () causes a fatal error. In other words, if you want to lose 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.