File references in PHP (include,require,include_once,require_once)

Source: Internet
Author: User
To apply an include statement to reference a file

Note: When you apply an external file using include, the external file is introduced and read only when the code executes to the include statement, and when an error occurs in the referenced external file, the system only gives a warning, and the entire PHP file continues to execute downwards.

Include ("top.php");

Include ("main.php");

Include ("bottom.php");

?>

Applying the Require statement to reference a file

Before the php file is executed, the PHP parser replaces the Require statement with the entire contents of the referenced file, and then composes the new PHP file with statements other than the Require statement, and then executes the program code in the new PHP file.

Note: Because the Require statement is equivalent to completely copying the contents of another source file into a file, it is generally placed at the beginning of the source file, referencing the public function files and public class files that need to be used.

The difference between the include statement and the Require statement

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

When you invoke a file using the Require statement, the external file is called immediately as soon as the program executes, and when an external file is called through the include statement, the external file is called only when the program executes to the statement.

Applying the Include_once statement to reference a file

Applying the Include_once statement detects whether the file has been applied to other parts of the page before importing it, and if so, does not repeatedly reference the file, and the program can only refer to it once.

For example, there are some custom functions in the file to be imported, so if you import this file repeatedly in the same program, an error will occur on the second import, because PHP does not allow the same name function to be repeatedly declared

Applying the require_once statement to reference a file

The require_once statement require the extension of the statement, its function is basically similar to the Require statement, the difference is that when the require_once statement is applied, the file to be referenced is not already referenced elsewhere in the program, if any, The file is not called repeatedly.

For example: While applying the require_once statement to refer to two identical files in the same page, only the first file is executed at the time of output, and the second referenced file will not be executed.

Differences between the use of include_once and require_once statements

The Include_once statement generates a warning when an error occurs while calling an external file during script execution, while the require_once statement causes a fatal error.

The purpose is to ensure that a contained file can only be included once, using these two statements to prevent accidental inclusion of the same library of functions, resulting in a duplicate definition of the function and generating an error.

The above describes the PHP file reference (include,require,include_once,require_once), including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.