Include (), require (), include

Source: Internet
Author: User
Tags require

First, the role

The include () and require () statements function to include and run the specified file.

Two, include () and require () difference

The two structures are exactly the same except how to handle failures. Include () generates a warning that the script will continue to run. Require () causes a fatal error and the script stops running.

In other words, if you want to stop processing the page when you encounter a missing file or encounter an error, use require (). Use include () if you want to continue processing the page if you encounter an error.

Note: When using include (), a syntax error in the containing file does not cause the program to stop until PHP 4.3.5, but after this version. That is, the script does not continue to execute when the include () file does not exist after PHP 4.3.5.

Third, include_once (), require_once ()

The include_once () statement contains and runs the specified file during the execution of the script. This behavior is similar to the include () statement, except that include_once () first determines whether the file has been included before and, if it is already contained, ignores the inclusion. Include_once () should be used in the case of nested inclusions, want to make sure it is only included once, to avoid problems such as function redefinition, variable assignment, and so on.

(Require_once () the same. )

Iv. issues to be noted

1, the path problem

Especially when nesting is included, be sure to note the path of the containing file.

For example, a file contains b files, b file contains c files, a,b,c files are not in the same folder, this time is often very easy to make mistakes.

Solution: Use an absolute path, that is, you can use the DirName (__file__) statement, which means to get the absolute path of the current script. such as: require_once (DirName (__file__). /config.php ');

2. Efficiency issues

Include_once (), require_once (), compared with include (), require (), are less efficient because they must at least determine if the file is included. The problem has been greatly improved in the PHP5 version, but there is still a difference in efficiency.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.