The files in PHP learning note 6--php contain include,require,include_once,require_once

Source: Internet
Author: User
Tags vars

The files in PHP contain include,require,include_once,require_once

File contains: Refers to the content of one file is included in another file, facilitates the reuse of code and so on.
There are 4 files in PHP with the following directives:
Include,include_once,require,require_once
Where: Include and require: Use once to include the file, while Include_once and require_once are: No matter in a script contains
How many times another script is loaded only once and not duplicated, which avoids problems such as the duplication of a file, the resulting function redefinition, and so on.
Include_once and Require_once will make a judgment before including the file, judging whether the currently required files are included, if
It has already been included in the waiver of inclusion, otherwise it is included.

The difference between include and require is that include attempts to include, if not included, a warning error, but does not affect the current own foot
The require must go to include the file, and the contained file must not be faulted, or it will terminate the execution of the current script itself.

With regard to the use of file inclusions, the PHP help manual mentions some areas of attention that can help you understand the mechanisms that files contain, thus avoiding unnecessary
Trouble:
1. The included file is searched by the path given by the parameter, and if no directory (only file name) is given, then the directory specified by include_path is searched.
。 If the file is not found under Include_path, the include is finally searched in the directory where the script file is called and in the current working directory. Such as
If the file is not found at the end, the include structure emits a warning, unlike require, which emits a fatal error.
2. When a file is included, the code contained in it inherits the range of variables in the row of the include. From there, the calling file is available at the line where the
Any variables that are used are also available in the called file. However, all functions and classes defined in the include file have global scope.
3. If include appears in a function in the calling file, all code contained in the called file will behave as if they were in that letter
The same as the number internally defined. So it will follow the variable range of the function. One exception to this rule is the Magic constants, which are
Parser-processed.
4. The include returns FALSE and warns when it fails. A successful inclusion returns 1 unless the return value is also given in the included file. OK
Use the return statement in the included file to terminate the execution of the program in the file and return the script that called it. It is also possible to return from the included file
return value. The return value of the include call can be obtained just like a normal function.
5. Because include is a special language structure, its parameters do not require parentheses. Be careful when comparing its return values:
if (Include (' vars.php ') = = = ' OK ') {//won ' t work, evaluated as
echo ' OK ';//include (' vars.php ') = = ' OK ')
}
If (include ' vars.php ') = = = ' OK ') {//Works
Echo ' OK ';
}
6. If a function is defined in the include file, these functions can be used independently of the main file, either before or after the return. If
The file is included two times and PHP 5 makes a fatal error because the function is already defined, but PHP 4 does not give an error to the function defined after the return. Recommended
Use include_once instead of checking to see if the file is included and conditionally returned in the include file.
7. When a file is included, the parser goes out of PHP mode at the beginning of the target file and enters HTML mode to resume at the end of the file. Since this
Cause, any code that needs to be executed as PHP code in the target file must be included in the valid PHP start and end tags.
PS: My understanding is that the inclusion of the file, the entire file will be included in the target file, and then to parse, based on the 3rd above, I know,
The magic constants are resolved before this, an exception.

The files in PHP learning note 6--php contain include,require,include_once,require_once

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.