Require and require in PHP

Source: Internet
Author: User
Tags require variable scope

This article is mainly on the PHP require and require_once The difference is introduced, the need for friends can come to the reference, I hope to help you

Include () and require (): The statement includes and runs the specified file.

Include () produces a warning and require () causes a fatal error. In other words, if you want to stop processing the page when you encounter a missing file, use require (). Include () is not the case, the script will continue to run.

The require_once () statement includes and runs the specified file during the execution of the script. This behavior is similar to the Require () statement, except that if the code in the file is already included, it is not included again.

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

The 1.include () function reads the specified file into and executes the program inside.

For example: include ('/home/me/myfile ');

The program code in the file being imported is executed, and these programs have the same variable scope (variable scope) as the location of the call to the include () function in the source file at the time of execution. You can import static files from the same server, and you can even use the include () and fopen () functions to import files from other servers.

The functions of the 2.include_once () function and include () are almost identical

The only difference is that the include_once () function first checks whether the file being imported has been imported elsewhere in the program, if you do, you will not be able to import the file again (this feature is sometimes important, for example, to import a file that declares some of your own well-defined functions. , if you import the file repeatedly in the same program, an error message will occur the second time you import it, because PHP does not allow functions of the same name to be repeated for the second time.

The 3.require () function reads the contents of the target file and replaces itself with the read content.

This read-and-replace action occurs when the PHP engine compiles your program code. Instead of what happens when the PHP engine starts executing the compiled program code (the PHP 3.0 engine works by compiling one line of execution, but it changes in PHP 4.0, PHP 4.0 is the first to put the whole Once the program code is compiled, the compiled program code is executed one at a time, and no program code is executed during the compilation. Require () is typically used to import static content, and include () is suitable for importing dynamic program code.

4. As with the include_once () function, the require_once () function will first check that the contents of the target file have been imported before, and if so, the same content will not be imported again.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.