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!';

图片转代码服务由CSDN问答提供 功能建议

我正在写一个有多个页面的小网站。 我想在每个页面上都有相同的页脚,但我不想每天手动更新10页HTML。 我想在每个HTML页面中对外部文件进行PHP调用(现在.php页面,感谢@ br14np),这样当我更新PHP文件时,所有页面 - 加载时 - 将显示相同的页脚文本 。

 &lt; p&gt;&lt;?php footertext.php?&gt;&lt; / p&gt; 
   
 
 

是我在猜测上述名称文件中的内容时的猜测,但无济于事。 (在footertext.php中,代码是:&lt;?php print(“Test numba one”)?&gt; )。

我该怎么做呢? 我更喜欢涉及PHP的答案。

更新:

这是我正在使用的确切代码。 一切都在同一个目录中。

主文件:

 &lt; html&gt; 
&lt; head&gt;  
&lt; / head&gt; 
 
&lt; body&gt; 
&lt; p&gt; 内容:&lt;?php包括“footertext.php?&gt;&lt; / p&gt; 
&lt; / body&gt; 
 
&lt; / html&gt; 
   
 
 

页脚内容:

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条)

相关推荐 更多相似问题