包含代码机制
include与echo
使用 include(或 require 等类似的文件包含函数)包含一个文件时,如果被包含的文件中有 echo 指令,那么 echo 输出的内容会在包含该文件的主文件中显示出来。
例如,假设有一个 included_file.php 文件,内容如下:
1 2 3 4
| <?php echo "This is a text"; include('included_file.php') ?>
|
而在文件include_file.php中,内容如下:
1 2 3
| <?php echo "This is included text"; ?>
|
当执行 included_file.php 时,输出结果为:
1 2
| This is a text This is included text
|
可以看到,included_file.php 中的 echo 输出内容在主文件中也显示出来了。
include括号里面的字符类型设置
include 函数的括号里面的字符类型设置有以下几种:
绝对路径
1
| include('/path/to/file.php');
|
相对路径
变量
1 2
| $file = 'file.php'; include($file);
|
包括在其中写入$_GET、$_POST、$_COOKIE、$_SERVER等超全局变量的变量名,也可以包含文件。
注意:
include和$_GET之间可以没有空格如include$_GET[‘file’]
协议
1
| include('http://example.com/remote.php');
|
php流包装器
1
| include('php://filter/read=convert.base64-encode/resource=file.txt');
|