dongyisa6254 2013-03-12 13:40
浏览 46
已采纳

在PHP网页中,从外部PHP读取文本以简化页面更新内容

I'm writing a small website that has multiple pages. I'd like to have the same footers on each page, but I don't want to manually update 10 pages of HTML everyday. I'd like to put a PHP call to an external file in each HTML page (now .php pages, thanks to @br14np) so that when I update the PHP file, all the pages - when loaded - will show the same footer text.

<p><?php footertext.php ?></p>

is my wild guess at loading the content in the file of the afformentioned name but to no avail. (In footertext.php the code is: <?php print("Test numba one") ?>).

How can I go about doing this? I'd prefer an answer involving PHP.

UPDATE:

This is the exact code I'm using. Everything is in the same directory.

Main File:

<html>
    <head> 
    </head>

    <body>
        <p> Content: <?php include "footertext.php ?></p>
    </body>

</html>

Footer Content:

echo 'Test numba TWO!';

  • 写回答

2条回答 默认 最新

  • douhuan4699 2013-03-12 13:43
    关注

    Use the include function. Just give it the path to your file. Example:

    <?php include "footertext.php"; ?>
    

    There are a few other functions that do similar things, such as require_once(). You can read more about that here.

    Response to update

    You're missing closing quotation marks after "footertext.php. Another tip that may help this situation is to turn on php error reporting. This will display any syntax or other errors on your page. Just insert the following code at the very top of your pages:

    <?php
      error_reporting(E_ALL);
      ini_set("display_errors", 1);
    ?>
    

    Also make sure you have opening and closing php tags (<?php ... ?>) in your footertext.php file.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)