Differences between include, include_once, require, and require_once statements in PHP files

Source: Internet
Author: User

 

PHP files include, include_once, require, and require_once statements.

1. Include ()

Include (/path/to/filename)

The include () Statement will contain a file at the location where it is called. A file contains the same content as the data copied to the file where the statement is located.

Parentheses can be ignored when include () is used.

You can execute the include () statement according to the conditions. The use of include () in conditional statements has a strange phenomenon. It must be enclosed in statement block braces or enclosed by other statements.

2. include_once ()

Include_once (filename)

The role of the include_once () function is the same as that of the include function, but it will first verify whether the file is included. If it already exists, revoke de_once is not executed. Otherwise, the file must be included. This is the same as include.

3. Require ()

Require (filename)

To a large extent, require () is the same as include. A template file is included in the location where the require call is located.

There are two important differences between require and include. First, regardless of the location of require, the file will be included in the script where require appears. For example, even if require is placed in an if statement whose calculation result is false, it still contains the specified file.

The second important difference is that when a require error occurs, the script stops running, and the script continues to run when the include is used.

4. require_once ()

Require_once (filename)

As the website grows bigger, some files may be repeatedly contained. This may not be a problem, but after modifying the variable of the included file, it is overwritten because it contains the original file again. This may not happen. Another problem may also occur, that is, the conflict of letters in the file. Use require_once to solve these problems.

The require_once function ensures that the file is only contained once. If you encounter require_once and try to include the same file later, it will be ignored.

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.