Home > Article > Backend Development > What is the function of include in php
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';".
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 torequire.
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 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 'vars.php'; echo "A $color $fruit"; } /* vars.php 在 foo() 范围内,所以 $fruit 在范围为不可用。 * * $color 能用是因为声明成全局变量。 */ foo(); // A green apple echo "A $color $fruit"; // A green ?>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 'http://www.example.com/file.txt?foo=1&bar=2'; // 无法执行;在本地文件系统中查找名为 “file.php?foo=1&bar=2” 的文件。 include 'file.php?foo=1&bar=2'; // 正常。 include 'http://www.example.com/file.php?foo=1&bar=2'; ?>
WARNINGRemote 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!