Included statements within PHP (include (), require (), include_once (), require_once ())

Source: Internet
Author: User
Tags php programming

Tag: Ack appears file contains parameter generated call name according to store

Background: Continue to summarize the functions in PHP.

In the general program, we often reuse some code, but if you simply copy the code, it will increase the redundancy of the code, not easy to run the program. As a result, we can isolate repetitive code, store it as an external file, and introduce it when it is used.

Referencing external files can reduce the reusability of code and is an important technique for PHP programming. and PHP provides us with 4 very simple inclusion statements. They allow any type of code to be reused. A file can be loaded into PHP using either of the Henan Opera.

One. Include () statement

When using an include () statement to include an external file, the external file is included only when the code executes to the statement, and when an error occurs in the contained external file, the system only gives a warning, and the entire PHP file continues to execute downwards. The syntax is as follows:

void include (string filename);

The parameter filename is the full path file name specified.

Two. Require () statement

The Require () statement, like the include () statement, is a call to an external file. The syntax is as follows:

void require (string filename);

Note: When you load a file using the Require () statement, it is executed as part of the PHP file, for example, by loading an HTML Web page file with the Require () statement, any PHP commands within the file will be processed, but, for example, if the PHP script is simply placed in an HTML page, It's not going to be processed.

Three. Include_once () statement

When using the include_once () function, it should be clear that it differs from the include () function, and the program will only be called once if the Include_once () function is called multiple times for the same file. The include_once () function is an extension of the include () function, and its function is almost identical to the include () function, except that the include_once () function detects whether the file was imported in the other part of the page before importing it. If you do, you will not import the file repeatedly. If you refer to it again in the case of a reference, the second import will cause an error. The syntax is as follows:

void include_once (string filename);

Note: How to determine the application of include () and include_once ()?

Should vary depending on the program, but it is generally recommended to use the include_once () statement because the application include_once () statement does not occur because the duplicate contains two identical file two errors.

Four. Require_once () statement

The require_once () statement is an extension of the require () statement. Functions are similar to require (). Attributes are similar to Include_once (). are only loaded once, if the load is repeated, will be error, but there are also written differences between the two, the specific differences will be cited at the end.

The syntax is as follows:

void require_once (string filsename);

Five. The difference between the include () statement and the Require () statement

The specific differences between the two are as follows:

1. When a file is called using the Require () statement, if the file is not found, the require () statement outputs an error message and immediately terminates the processing of the script, while the include () statement outputs a warning when no file is found, but does not terminate the processing of the script.

2. When invoking a file using the Require () statement, the external file is called immediately whenever the program executes, and when an external file is invoked through the include () statement, the external file is called only when the program executes to the statement.

Six. The difference between include_once () and require_once ()

The use of both is to ensure that a contained file can only be included once, using these two statements to prevent accidental introduction of the file, but the difference is: include_once () statement when the script is executed during the invocation of an external file error occurs when a warning is generated, and require_once () Statement causes a fatal error.

Included statements within PHP (include (), require (), include_once (), 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.