在 html5 代码中包含 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21653901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including php in a html5 code
提问by user3288578
I've a php code that I want to include in my html5 code. The php code looks like this:
我有一个 php 代码,我想包含在我的 html5 代码中。php 代码如下所示:
<?php
$accounts = mysql_connect("localhost","root","tungdesign") or die(mysql_error());
mysql_select_db("database", $accounts);
$sql ="SELECT * FROM users";
$result = mysql_query($sql, $accounts);
while($row = mysql_fetch_array($result)){
$username=$row['Username'];
$first=$row['Firstnam'];
echo $username . "'s first name is ". $first . "<br>";
}
?>
This code I been trying to include in my html5 code using the include command, so in the body of the html doc I've added this:
这段代码我一直在尝试使用 include 命令包含在我的 html5 代码中,因此在 html 文档的正文中我添加了以下内容:
<?php include("About.php");?>
When I just open the php file it works, but included in the html5 file, I can't see the output. Is it because of the echo in php or why can't it be displayed in the website?
当我打开它的 php 文件时,它可以工作,但包含在 html5 文件中,我看不到输出。是因为php中的echo还是为什么在网站上显示不出来?
First time I've asked a question here and I hope you guys can give me an answer. Also I been trying to search for an answer for a couple of hours, so this is my last hope.
第一次在这里提问,希望大家能给我一个答案。此外,我一直试图寻找几个小时的答案,所以这是我最后的希望。
回答by azngunit81
when you say HTML5 code - what do you mean? because PHP needs to be executed with .php files.
当你说 HTML5 代码时 - 你是什么意思?因为 PHP 需要使用 .php 文件执行。
if your invoking HTML5 is a .html - it wont execute.
如果您调用的 HTML5 是 .html - 它不会执行。
what you need to do is use html5 inside php
你需要做的是在 php 中使用 html5
so index.php should be:
所以 index.php 应该是:
<!DOCTYPE html>
<?php
include('About.php');
?>
</html>
but index.html
但是 index.html
<!DOCTYPE html>
<?php
include('About.php');
?>
</html>
wont yield anything because the .html wont understand what to do with it.
不会产生任何东西,因为 .html 不明白如何处理它。
回答by Quentin
It doesn't matter what else is in the file. PHP just treats it as text to be output until it sees a <?php.
文件中还有什么并不重要。PHP 只是将其视为要输出的文本,直到它看到<?php.
If the code isn't executing then your server isn't processing the file for PHP directives.
如果代码没有执行,那么您的服务器没有处理 PHP 指令的文件。
The most likely causes for this are:
最可能的原因是:
- You aren't loading it through a web server.
- You haven't installed PHP.
- You have given the file a
.htmlextension but not changed the server configuration to treat.htmlfiles as PHP.
- 您不是通过 Web 服务器加载它。
- 你还没有安装 PHP。
- 您已为文件指定了
.html扩展名,但未更改服务器配置以将.html文件视为 PHP。
回答by webdev-dan
You can put HTML in PHP – not the other way. I mean – if your server doesn't parse .htmfiles with PHP parser (you need to change server settings to do that) – it will be just HTML and therefore PHP embedded in its code will not work.
您可以将 HTML 放入 PHP - 而不是其他方式。我的意思是——如果你的服务器不.htm使用 PHP 解析器解析文件(你需要更改服务器设置来做到这一点)——它将只是 HTML,因此嵌入在其代码中的 PHP 将无法工作。

