PHP基础之包含文件

包含文件

  • 应用场景
    • 一套网站中,包含复用的头和脚,可以运用引入的方式,避免重写

包含文件的方式

  • 包含文件的方式
    • require 包含多次
    • include 包含多次
    • require_once 包含一次
    • include_once 包含一次
<?php
    include './head.html';
    require './head.html';
?>

包含文件注意事项

  • 包含文件注意事项

    • require遇到错误抛出error类别的错误,停止执行
    • include遇到错误抛出warning类型的错误,继续执行
    • require_once、include_once只能包含一次
    • HTML类型的包含页面中存在PHP代码,如果包含到PHP中是可以被执行的
    • 包含文件相当于把包含文件中的代码拷贝到主文件中执行,魔术常量除外,魔术常量获取的是所在文件的信息。
    • 包含在编译时不执行、运行时加载到内存、独立编译包含文件
  • 包含文件的路径

    • require 'head.html' 受include_path配置影响
    • require './head.html' 在当前目录下查找
    • require '../head.html' 在上一级目录下查找
  • include_path 配置

    • 如果包含文件的目录结构比较复杂,可以将包含的路径设置成include_path
    • 这样包含就只要写文件名就可以了
    • include_path可以设置多个,路径之间用分号隔开
<?php
  # 设置include_path
  set_include_path('c:\aa\bb\cc\dd');  
  # 受include_path配置影响
  require 'head1.html';
  require 'head2.html';
?>
<?php
  set_include_path('c:\aa\bb\cc\dd; d:\\');
?>

正斜(/) 反斜(\)

  • 正斜(/) 反斜(\)
    • 正斜(/) web中目录分隔用正斜
      • http://www.sina.com/index.php
    • 反斜(\) 物理地址的分隔用反斜
    • windows中物理地址正斜和反斜都可以
      • c:\web1\aa
posted @ 2020-11-14 09:35  wing1377  阅读(138)  评论(0)    收藏  举报