The difference between file-containing statements in PHP

Source: Internet
Author: User
Tags contains expression functions include php and php script require

PHP has four functions that contain files: include (), include_once (), require (), and require_once (). Figuring out their differences is one of the basics of learning PHP and avoids the unnecessary hassle of writing code.

include ()

1. Mode of invocation: include ("/path/to/filename")

2. Note: the include () statement will contain the file specified by the parameter where it is invoked, with the effect of copying the contents of a file where include () appears. When using include (), parentheses can be ignored, such as include "/path/to/filename".

3. Traps: Through If...else ... Conditional statement to determine whether to include a file is a strange phenomenon. Such as

<?php

if (expression)

Include ("/path/to/filename");

Else

Include ("/path/to/anotherfilename");

?>

The above code may be wrong to run. Why is that? The Include () function simply copies the contents of the file to where the include () function appears, if the file contains multiple lines of PHP statements without using {} to compose the code quickly? The whole if...else ... The logic is messed up. So, this code should write this:

<?php

if (expression) {

Include ("/path/to/filename");

}

else{

Include ("/path/to/anotherfilename");

}

?>

This will ensure that the included files are in the code fast.

include_once ()

1. Mode of invocation: include_once ("filename")

2. Note: As the name implies, contains only one time the file. That is, if the file is already contained in the context, it is no longer included.

3. Traps: Have the same traps as include () functions.

require ()

1. Mode of invocation: Require ("filename")

2. Note: In addition to the following two points, the function is the same as include (): (1) regardless of where require () appears in the program fragment, it can include the file. Consider the following procedure:

<?php

if (false) {

Require ("/path/to/filename");

}

else{

Require ("/path/to/anotherfilename");

}

?>

The above statement includes both filename and anotherfilename two files, even if the first condition tests a condition that is false. (2) if the require () error occurs (for example, the require file does not have an error), the PHP script program will stop executing, but include () does not occur.

3. Trap: Have the same trap as include ().

require_once ()

1. Mode of invocation: require_once ("filename")

2. Note: Other functions are the same as require () except that they contain only one file at a time.

3. Trap: Have the same trap as require ().







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.