Php include and include_once, require and require_once

Source: Internet
Author: User
Phpinclude and include_once, require and require_once * Include () and include_once ()
* Require () and require_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 () functionThe 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.
I personally use the require () function to import copyrights, static text or other programs that do not contain variables, or code that can be correctly executed only by other programs that have been executed.

What is the difference between include and require in php?
Normally, there is no difference.
When the file to be loaded does not exist, include will give a warning, and then continue to run. while require will give a fatal error, directly end the script

The file contained by require () cannot contain a control structure and cannot use statements such as return. Using the return statement in files contained in require () produces processing errors.

Unlike the include () statement, the require () statement unconditionally reads the content of the files it contains, regardless of whether these statements are executed. Therefore, if you want to include different files according to different conditions, you must use the include () statement. Of course, if the statement where require () is located is not executed, the statements in the file contained in require () will not be executed.

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.