【问题标题】:Why can't I see my php code rendered on page?为什么我在页面上看不到我的 php 代码?
【发布时间】:2013-11-13 10:18:03
【问题描述】:

刚刚开始使用 php。在 index.php 我有

<?php
 echo 'test';
?>

当我转到 localhost:8080/php/index.php 时,我看到了“测试”。但是我用这段代码制作了一个 HTML 文件 index.html:

<html>
<body>
<?php include 'index.php'; ?>
<h1>Welcome to my home page!</h1>
</body>
</html>

但我看到的只是“欢迎来到我的主页”,没有“测试”。想知道我在这里缺少什么。

谢谢!

【问题讨论】:

  • 哈哈,我知道这是一个菜鸟问题,但为什么这被否决了,所以我以后可以避免这种习惯?

标签: php html include render


【解决方案1】:

将文件扩展名从.html更改为.php

index.html 更改为index.php


注意:您不能在 .html 文件中执行 php 代码。

还有一件事,您需要更改两个文件名。因为您将 index.html 更改为 index.php 并且已经存在另一个同名文件。

这样做吧。

index_inc.php

<?php
 echo 'test';
?>

index.php

<html>
<body>
<?php include 'index_inc.php'; ?>
<h1>Welcome to my home page!</h1>
</body>
</html>

【讨论】:

  • 或将特定的html文件或所有html文件解析为php
  • “注意:您不能在 .html 文件中执行 php 代码。” - 不完全正确。您可以告诉 Apache 将 .html 文件视为 PHP。我会让你谷歌一下。
【解决方案2】:

您的文件扩展名为.html,因此不会执行php代码。 如果你真的想让.html文件执行php代码,你可以写一个.htaccess文件 并添加行

AddType application/x-httpd-php .php .html

【讨论】:

  • 你可以,通过更改公共设置。
  • “您不能在 html 文件中使用 php 代码。” - 不正确。我会让你谷歌“把 html 当作 php”然后回来编辑你的答案以反映你的发现。
  • 谢谢。我获得了新知识。
猜你喜欢
  • 2022-11-20
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 2011-06-25
  • 2021-03-16
  • 2012-09-10
  • 2015-09-26
  • 2020-02-03
相关资源
最近更新 更多