将 PHP 文件包含到 HTML 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20105029/
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
Include PHP file into HTML file
提问by adlowe07
I'm working on a project that may have to change the same content on all html pages. So I figured I would create a php file and only have to change that so it changes on all pages over the web.
我正在处理一个项目,该项目可能必须更改所有 html 页面上的相同内容。所以我想我会创建一个 php 文件,只需要更改它,以便它在网络上的所有页面上更改。
The files are saved as:
文件保存为:
index.html
number.php
EXAMPLE:
例子:
------------------------(HTML FILE)----------------------------
--------------(HTML 文件)-------------- ------
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Phone Number</h1>
<?php include('number.php') ?>
</body>
</html>
------------------------(PHP FILE)----------------------------
----------------------(PHP 文件)-------------- ------
<?php
echo 4895553268;
?>
What could I do without changing the file extension of all my html's into php. I've found that works but I would like to only change the code in the html page. I've tried include require tags and that didn't work so I tried script tags and can't seem to make it work right.
如果不将所有 html 的文件扩展名更改为 php.ini ,我该怎么办?我发现这可行,但我只想更改 html 页面中的代码。我试过包含需要标签,但没有用,所以我尝试了脚本标签,但似乎无法使其正常工作。
回答by Rob Baillie
In order to get the PHP output into the HTML file you need to either
为了将 PHP 输出放入 HTML 文件中,您需要
- Change the extension of the HTML to file to PHP and include the PHP from there (simple)
- Load your HTML file into your PHP as a kind of template (a lot of work)
- Change your environment so it deals with HTML as if it was PHP (bad idea)
- 将 HTML 文件的扩展名更改为 PHP 并从那里包含 PHP(简单)
- 将您的 HTML 文件作为一种模板加载到您的 PHP 中(大量工作)
- 改变你的环境,让它像处理 PHP 一样处理 HTML(坏主意)
回答by Om Bissa
Create a .htaccess file in directory and add this code to .htaccess file
在目录中创建一个 .htaccess 文件并将此代码添加到 .htaccess 文件中
AddHandler x-httpd-php .html .htm
or
或者
AddType application/x-httpd-php .html .htm
It will force Apache server to parse HTML or HTM files as PHP Script
它将强制 Apache 服务器将 HTML 或 HTM 文件解析为 PHP 脚本
回答by Mike Brant
You would have to configure your webserver to utilize PHP as handler for .htmlfiles. This is typically done by modifying your with AddHandlerto include .htmlalong with .php.
您必须配置您的网络服务器以使用 PHP 作为.html文件处理程序。这通常是通过修改您做AddHandler,包括.html沿.php。
Note that this could have a performance impact as this would cause ALL .html files to be run through PHP handler even if there is no PHP involved. So you might strongly consider using .phpextension on these files and adding a redirect as necessary to route requests to specific .html URL's to their .php equivalents.
请注意,这可能会对性能产生影响,因为这会导致所有 .html 文件通过 PHP 处理程序运行,即使不涉及 PHP。因此,您可能会强烈考虑.php在这些文件上使用扩展名,并根据需要添加重定向以将请求路由到特定的 .html URL 到它们的 .php 等效项。
回答by McAden
You'll have to configure the server to interpret .htmlfiles as .phpfiles. This configuration is different depending on the server software. This will also add an extra step to the server and will slow down response on all your pages and is probably not ideal.
您必须将服务器配置为将.html文件解释为.php文件。此配置因服务器软件而异。这也会给服务器增加一个额外的步骤,并且会减慢所有页面的响应速度,这可能并不理想。

![php Codeigniter:foreach 方法或结果数组?[型号+查看]](/res/img/loading.gif)