drdu53813 2016-06-27 13:50
浏览 46
已采纳

如何将PHP文件包含在已包含在许多其他页面PHP中的文件中

Can it be possible to include a PHP file into another file which is already included in many other files on different paths

For example I have the following files and directories:

domain/include/header.php
domain/include/body.php
domain/include/user_data.php

header.php file contains user_data.php and header.php file itself contained in many other pages on different location:

domain/index.php
domain/home/index.php
domain/home/user/index.php

I want to access my user_data.php file within one(i.e header.php) and don't want to include it again and again in each file.

How is this possible?

  • 写回答

2条回答 默认 最新

  • dongwu4834 2016-06-27 13:56
    关注

    Yeah why not. You can create master include file and that file includes all other files from the directory.

    So your header.php file will look something like this,

    <?php
        include('user_data.php'); // Assuming file is in same location.
    

    Now include only header.php file where every you want, it will automatically includes user_data.php along with it.

    Extra:

    This is useful for including database connection files.

    You can also use this file include root level JS and CSS so you don't have include those assets on every page.


    Using include_once() is good practice, as if you have included user_data.php file somewhere in other files and it also gets include from the hedaer.php file, It won't get included twice.

    As that is all include_once() does, it includes the file content only once per execution.

    The only difference between include() and include_once() that if the code from a file has already been included, it will not be included again, and include_once returns TRUE.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码