Php -- require/include/require_once/include_once

Source: Internet
Author: User
Require and include are almost the same, except for different methods of handling failures. Require generates an E_COMPILE_ERROR-level error when an error occurs. In other words, it will cause the script to stop and include will only generate a warning (E_WARNING), and the script will continue to run.


Require and include are almost the same, except for different methods of handling failures. Require generates an E_COMPILE_ERROR-level error when an error occurs. In other words, it will cause the script to stop and include will only generate a warning (E_WARNING), and the script will continue to run.


The include statement contains and runs the specified file.

The following documents also apply to require:

The contained file is first searched based on the path given by the parameter. If no directory is provided (only the file name), it is searched based on the directory specified by include_path. If this file is not found in include_path, include is finally located in the directory where the script file is called and in the current working directory. If the file is not found at the end, the include structure will issue a warning; this is different from require, the latter will issue a fatal error. (For details about include_path, refer to this article: Options and configurations of PHP extension)

If the path is defined, whether it is an absolute path (starting with a drive letter or \ in Windows, starting with/in Unix/Linux) or the relative path of the current directory (starting. or ..) -- include_path will be completely ignored. For example, if a file starts with ../, the parser searches for the file in the parent directory of the current directory.

When a file is included, the code contained in the file inherits the variable range of the row where the include is located. From this point on, any variables available to the calling file in this row are also available in the called file. However, all functions and classes defined in the inclusion file have a global scope.

Example #1 basic include Example


If include appears in a function in the calling file, all the code contained in the called file will behave as they are defined inside the function. So it will follow the variable range of the function. An exception to this rule is a magic constant that has been processed by the parser before inclusion occurs.

Example #2 function inclusion


When a file is contained, the parser disconnects the target file from the PHP mode to the HTML mode and restores it to the end of the file. For this reason, any code in the target file that needs to be executed as PHP code must be included in the valid PHP start and end tags.

If "URL fopen wrappers" is activated in PHP (default configuration), you can use URL (via HTTP or other supported encapsulation protocols-see Supported protocols and encapsulation protocols) instead of a local file to specify the file to be included. If the target server interprets the target file as PHP code, you can use the URL request string applicable to http get to pass variables to the included file. Strictly speaking, this is not the same as the variable space that contains a file and inherits the parent File. the script file is actually running on a remote server, and the local script includes the result.

Before version 4.3.0, PHP of WarningWindows does not support remote file access through this function, even if allow_url_fopen is enabled.

Example #3 include through HTTP


Security warning

Remote files may be processed by the remote server (depending on the file suffix and whether the remote server is running PHP), but a valid PHP script must be generated, because it will be processed by the local server. If the files from the remote server should be run remotely and only output results, it is better to use the readfile () function. In addition, be careful to ensure that remote scripts generate valid and necessary code.

Processing return value: if the request fails, include returns FALSE and generates a warning. If a successful inclusion is successful, 1 is returned unless the returned value is provided in the contained file. You can use the return statement in the included file to terminate the execution of the program in the file and return the script that calls it. You can also return values from included files. The return value of the include call can be obtained just like that of a common function. However, this does not work when the remote file is included, unless the remote file output has a valid PHP start and end mark (like any local file ). You can define the required variable in the tag. the variable is available after the file is included.

Because include is a special language structure, its parameters do not need parentheses. Pay attention when comparing the returned values.

Example #4 compare the return values of include


Example #5 include and return statements

Return. php

$ Var = 'php ';

Return $ var;


Noreturn. php

$ Var = 'php ';


Testreturns. php

$ Foo = include 'return. php ';

Echo $ foo; // prints 'php'

$ Bar = include 'noreturn. php ';

Echo $ bar; // prints 1


The value of $ bar is 1 because the include operation is successful. Note the differences in the above example. The first one uses return in the contained file, while the other does not. If the file cannot be contained, FALSE is returned and an E_WARNING warning is issued.

If a function is defined in the include file, these functions can be used independently in the main file, whether defined before or after return. If the file is contained twice, PHP 5 issues a fatal error because the function has been defined, but PHP 4 does not report an error for the function defined after return. We recommend that you use include_once instead of checking whether the file is included and conditional return is included in the file.

Another way to "include" The PHP file to a variable is to use the output control function and include to capture its output, for example:

Example #6 use the output buffer to include a PHP file into a string


To automatically include files in the script, see auto_prepend_file and auto_append_file configuration options in php. ini.

Note: A language constructor rather than a function cannot be called by a variable function.


(PHP 4, PHP 5)

The require_once statement is the same as the require statement. The only difference is that PHP checks whether the file has been included.


(PHP 4, PHP 5)

The include_once statement contains and runs the specified file during script execution. This behavior is similar to the include statement. The only difference is that if the file has already been included, it will not be included again. As the statement name implies, it will only be included once.

Include_once can be used when the same file may be contained more than once during script execution. to ensure that it is only included once, you can avoid function redefinition and variable re-assignment.


In PHP 4, the behavior of _ once varies in operating systems (such as Windows) that are case-insensitive. for example:

Example #1 running de_once in PHP 4 running in case-insensitive operating systems


This behavior is changed in PHP 5. for example, in Windows, the path is normalized first, so C: \ PROGRA ~ The implementation of 1 \ A. php is the same as that of C: \ Program Files \ a. php, and the file will only be included 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: 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.