Brief introduction to include, include_once, require, and require_once statements in PHP

Source: Internet
Author: User
Include () and require () statements include and run the specified file. The two structures are exactly the same on the contained files, and the only difference is the handling of errors. When the require () statement encounters an error or the contained file does not exist, it stops and returns an error. Include (): continue 1. include statement

The include statement tells PHP to extract a specific file and load all its content.

<? Phpinlude "fileinfo. php"; // add other code here?>

2. include_once statement

Every time you use the include statement, it will re-import the requested file, even if the file has been imported. For example, assume fileinfo. the PHP file contains many functions. We use the include statement to import it to an existing file, and then we import another file containing fileinfo. php files, through nesting, we have added fileinfo. if the php file is imported twice, an error occurs because we try to define variables or functions with the same name multiple times. To avoid this, we use the include_once statement instead of the include statement.

<? Phpinclude_once "fileinfo. php"; // add other code here?>

If another include or include_once statement is encountered in the same file, PHP checks whether it has been imported. If yes, ignore it.

3. require and require_once statements

The potential problem with using include and include_once statements is that PHP will only try to import the file to be requested for import. even if the file is not found, the program will still execute.
When we absolutely need to import a file, we use the require statement, which is the same for the reason of using the require_once statement, so we will not go into details here.

<? Phprequire_once "fileinfo. php"; // add other code here?>

In general, we should stick to the require_once statement.

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.