Php--require/include/require_once/include_once

Source: Internet
Author: User

Require

Require and include are almost exactly the same, in addition to handling failures differently. Require generates an E_COMPILE_ERROR level error when an error occurs, in other words, causes the script to abort and the include only generates a warning (e_warning), and the script continues to run.

Include

The include statement contains and runs the specified file.

The following documents also apply to require:

The included file is searched by the path given by the parameter, and if no directory (only the file name) is given, it is searched according to the directory specified by Include_path. 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. If the file is not found at the end, the include structure emits a warning, unlike require, which emits a fatal error. (For Include_path, refer to this article: PHP extension options and configuration information)

If a path is defined-whether it is an absolute path (under Windows with a drive letter or \ Start, under Unix/linux) or a relative path to the current directory (in. Or.. Start)--include_path are completely ignored. For example, a file with: /start, the parser looks for the file under the parent directory of the current directory.

When a file is contained, the code contained in it inherits the scope of the variable for the row in which the include is located. From there, any variables that are available to the calling file at that line are also available in the called file. However, all functions and classes defined in the include file have global scope.

Example #1 Basic include example

vars.php<?php    $color = ' green ';    $fruit = ' Apple ';? >test.php<?php    echo "a $color $fruit";//a    include ' vars.php ';    echo "A $color $fruit"; A Green apple?>

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

The Example #2 function contains

<?phpfunction foo () {    global $color;    Include ' vars.php ';    echo "A $color $fruit";} /* vars.php is under the scope of foo () so * * $fruit is not available outside of this * * scope. $color is because we declared it * * as global. */foo (); A green Appleecho "a $color $fruit"; A green?>

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. For this reason, any code in the destination 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 (the default configuration), you can specify which files to include by using URLs (via HTTP or other supported encapsulation protocols-see supported protocols and encapsulation protocols) instead of local files. If the destination server interprets the destination file as PHP code, you can pass the variable to the included file with the URL request string for the HTTP GET. Strictly speaking, this is not the same as a variable space that contains a file and inherits the parent file; The script file is actually already running on the remote server, and the local script includes its results.

The warningwindows version of PHP does not support access to remote files through this function prior to version 4.3.0, even if Allow_url_fopen is enabled.

Example #3 include via HTTP

<?php/* This    example assumes, www.example.com is configured to parse. PHP * * files and not.     txt files. Also, ' Works ' here means the variables *     * $foo and $bar is available within the included file. *    //Won ' t Work File.txt wasn ' t handled by www.example.com as PHP    include ' http://www.example.com/file.txt?foo=1&bar=2 ';    Won ' t work; Looks for a file named ' file.php?foo=1&bar=2 ' on the    //local filesystem.    Include ' file.php?foo=1&bar=2 ';    Works.    Include ' http://www.example.com/file.php?foo=1&bar=2 ';    $foo = 1;    $bar = 2;    Include ' file.txt '; Works.    Include ' file.php '; Works.? >

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 run at the far end and only output the results, then use the ReadFile () function better. Also take extra care to ensure that the remote scripts produce legitimate and required code.

Process return value: Include returns FALSE and warns when failure occurs. A successful inclusion returns 1 unless the return value is also given in the included 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 called it. It is also possible to return a value from a contained file. The return value of the include call can be obtained just like a normal function. However, this does not work with remote files unless the remote file's output has a valid PHP start and end tag (as with any local file). You can define the desired variable within the tag, which is available after the file is contained.

Because include is a special language structure, its parameters do not require parentheses. Be careful when comparing the return value.

Example #4 Compare the return value of include

<?php//won ' t work, evaluated as include (' vars.php ') = = ' OK '), i.e. include (') if (Include (' vars.php ') = = ' OK ') {    Echo ' OK ';} Worksif (include ' vars.php ') = = ' OK ') {    echo ' OK ';}? >

Example #5 include and return statements

return.php
<?php

$var = ' PHP ';

return $var;

?>

noreturn.php
<?php

$var = ' PHP ';

?>

testreturns.php
<?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 was successfully run. Note the differences in the above examples. The first one uses a return in the contained file and the other does not. Returns FALSE and emits a e_warning warning if the file cannot be included.

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, 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. It is recommended to use include_once instead of checking to see if the file is included and conditionally returned in the include file.

Another way to "include" a PHP file into a variable is to use an output control function in conjunction with an include to capture its output, for example:

Example #6 Use output buffering to include a PHP file in a string

<?php$string = get_include_contents (' somefile.php '); function get_include_contents ($filename) {    if (Is_file ($ FileName) {        ob_start ();        Include $filename;        $contents = Ob_get_contents ();        Ob_end_clean ();        return $contents;    }    return false;}? >

To automatically include files in the script, see Auto_prepend_file and Auto_append_file configuration options in php.ini.

Note: Because it is a language constructor and not a function, it cannot be called by a mutable function.

Require_once

(PHP 4, PHP 5)

The require_once statement is exactly the same as the Require statement, except that PHP checks to see if the file has already been included, and if it does, it will not contain

Include_once

(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, except that if the file is already contained, it will not be included again. As the name of this statement implies, it is included only once.

Include_once can be used to make sure it is included only once to avoid problems such as function redefinition, variable re-assignment, and so on, when the same file is likely to be included more than once during script execution.

Note:

In PHP 4, the behavior of _once differs in the case-insensitive operating system (such as Windows), for example:

Example #1 include_once Running in a case-insensitive operating system in PHP 4

<?phpinclude_once "a.php"; This will include a.phpinclude_once "a.php"; This will again contain the a.php! (PHP 4 only)?>

This behavior is changed in PHP 5, for example, paths are normalized in Windows, so c:\progra~1\a.php and C:\Program files\a.php implementations, files are only included once.

  • Related Article

    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.