What is the function of include in php

藏色散人
Release: 2023-03-14 16:10:02
Original
3545 people have browsed it

The function of include in php is to include and run the specified file. The included file is first searched according to the path given by the parameter. If no directory is given, it is searched according to the directory specified by include_path. Examples of its use are such as "include 'vars.php';".

What is the function of include in php

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

What is the function of include in php?

include (PHP 4, PHP 5, PHP 7, PHP 8)

## The #include expression includes and runs the specified file.

The following documents also apply to

require.

The included file is first searched according to the path given by the parameter. If no directory is given (only the file name), it is searched according to the directory specified by include_path. If the file is not found under include_path, include will finally search in the directory where the calling script file is located and the current working directory. The include structure will emit an E_WARNING if the file is not found at the end; this is different from require, which will emit an E_ERROR.

Note that if the file is inaccessible, both include and require will issue an additional E_WARNING before issuing the final E_WARNING or E_ERROR respectively.

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

For more information on how PHP handles include files and include paths, see the documentation in the include_path section.

When a file is included, the code contained in it inherits the variable scope of the line where the include is located. From that point on, any variables available in the calling file at that line are also available in the called file. However, all functions and classes defined in include files have global scope.

Example #1 Basic include example

vars.php

<?php
$color = &#39;green&#39;;
$fruit = &#39;apple&#39;;
?>
Copy after login

test.php

<?php
echo "A $color $fruit"; // A
include &#39;vars.php&#39;;
echo "A $color $fruit"; // A green apple
?>
Copy after login

If include appears in the call file, all code contained in the called file will behave as if it were defined inside the function. So it will follow the variable scope of that function. The one exception to this rule is magic constants, which are processed by the parser before inclusion occurs.

Example #2 Includes in functions

<?php
function foo()
{
    global $color;
    include &#39;vars.php&#39;;
    echo "A $color $fruit";
}
/* vars.php 在 foo() 范围内,所以 $fruit 在范围为不可用。 *
 * $color 能用是因为声明成全局变量。 */
foo();                    // A green apple
echo "A $color $fruit";   // A green
?>
Copy after login

When a file is included, the parser leaves PHP mode and enters HTML mode at the beginning of the target file, to Restore at end of file. For this reason, any code in an object file that needs to be executed as PHP code must be included within valid PHP start and end tags.

If "URL include wrappers" are enabled in PHP, you can use a URL (via HTTP or other supported wrapping protocols - see Supported Protocols and Wrapping Protocols) instead of a local file to specify the content to be included. document. If the target server interprets the target file as PHP code, you can pass variables to the included file using the URL request string for HTTP GET. This is not strictly the same thing as containing a file and inheriting the variable space of the parent file; the script file has actually been run on the remote server, and the local script includes its results.

Example #3 include over HTTP

<?php
/* 这个示例假定 www.example.com 配置为解析 .php 文件而不解析 .txt 文件。 *
 * 此外 “Works” 意味着 $foo 和 $bar 变量在包含的文件中是可用的。         */
// 无法执行; file.txt 没有被 www.example.com 当作 PHP 处理。
include &#39;http://www.example.com/file.txt?foo=1&bar=2&#39;;
// 无法执行;在本地文件系统中查找名为 “file.php?foo=1&bar=2” 的文件。
include &#39;file.php?foo=1&bar=2&#39;;
// 正常。
include &#39;http://www.example.com/file.php?foo=1&bar=2&#39;;
?>
Copy after login

WARNING

Remote files may be processed by the remote server (depending on the file suffix and (depends on 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 a file from a remote server should be run remotely and only output the results, it is better to use the readfile() function. Also take extra care to ensure that remote scripts produce legal and required code.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What is the function of include in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template