PHP 脚本无法在 HTML 文件中运行

发布于 2024-08-19 21:39:01 字数 343 浏览 1 评论 0原文

我是 PHP 新手。我安装了 XAMPP 并运行了 Apache。我在 XAMPP 的 htdocs 中创建了 helloworld.php 并让 PHP 显示在我的浏览器中。我的问题是,为什么 HTML 文件中的 PHP 脚本没有在浏览器中显示?我从来没有单独安装过 PHP。我也应该安装它吗?会和XAMPP冲突吗?我的代码如下。任何帮助将不胜感激。提前致谢:

<html>
    <body>
        <?php
            echo "Hello PHP World";
        ?>
    </body>
</html>

I'm new to PHP. I installed XAMPP and have Apache running. I created helloworld.php in XAMPP's htdocs and got PHP to display in my browser. My question is, why does my PHP script in my HTML file not display in my browser? Ive never installed PHP on its own. Should I also install it? Would it conflict with XAMPP. My code is below. Any assistance will be appreciated. Thanks in advance:

<html>
    <body>
        <?php
            echo "Hello PHP World";
        ?>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌梦深 2024-08-26 21:39:01

我假设您正在尝试在 .html 文件中使用 php?
尝试添加 .htaccess 文件或使用以下行更改 apache 配置:

AddHandler application/x-httpd-php .html

I assume you are trying to use php inside .html file?
Try adding .htaccess file or changing apache config with the following line:

AddHandler application/x-httpd-php .html
绝不服输 2024-08-26 21:39:01

XAMPP 已经包含 PHP,但除非您以 .php 结尾脚本名称,否则 PHP 引擎不太可能处理它。

XAMPP already includes PHP, but unless you end the script name with .php it is unlikely to be processed by the PHP engine.

停顿的约定 2024-08-26 21:39:01

停止 apache 服务,然后在 c:\xampp\apache\conf\httpd.conf 部分中添加一项更改,添加...

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

重新启动 apache!

这看起来像是当前 win 32 位 xampp 发行版中的一个重要“功能”。

Stop the apache service, then add one change in c:\xampp\apache\conf\httpd.conf in the section by adding...

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

Restart apache!

This looks like a big fat 'feature' in the current xampp distribution for win 32-bit.

七月上 2024-08-26 21:39:01

您应该在 http conf 中添加 mime 类型
例如在 apache 中的 httpd.conf
入口

<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig "conf/mime.types"
   .......
    AddType application/x-httpd-php .html .htm
   AddType text/html .shtml

    AddOutputFilter INCLUDES .shtml
</IfModule>

You should add mime type at http conf
for instance in apache at httpd.conf
entry

<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig "conf/mime.types"
   .......
    AddType application/x-httpd-php .html .htm
   AddType text/html .shtml

    AddOutputFilter INCLUDES .shtml
</IfModule>
甜`诱少女 2024-08-26 21:39:01

apache 的 php 模块将自身注册为 mime 类型 application/x-httpd-php 的处理程序。配置文件 apache\conf\extra\httpd-xampp.conf 包含的行

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>

告诉 apache 所有以 .php 作为扩展名的文件都将由 application/x-httpd-php< 的处理程序进行处理/i>.
如果您(确实)希望 .html 文件也由 php 模块处理,则必须为 .html 扩展添加类似的内容。 (还有其他方法可以告诉 apache 哪个扩展名映射到哪个 mime 类型/处理程序。但是 FilesMatch/SetHandler 是很好。)
如果您只想为一个目录启用此“功能”,您可以使用 。 htaccess 文件 来更改该目录(及其子目录)的配置。

The php module for apache registers itself as handler for the mime type application/x-httpd-php. And the configuration file apache\conf\extra\httpd-xampp.conf contains the lines

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>

which tells the apache that all files having .php as name extension are to be processes by the handler for application/x-httpd-php.
If you (really) want to have your .html files handled by the php module as well you have to add something similar for .html extensions. (there are other methods to tell the apache which extension maps to which mime type/handler. But FilesMatch/SetHandler is fine.)
If you want to enable this "feature" for only one directory you can use an .htaccess file to change the configuration for that directory (and its subdirectories).

感情洁癖 2024-08-26 21:39:01

太过分了。所有这些建议让我在大约 5 个小时的时间里走上了错误的道路。 JK,但我确实读了很多谷歌搜索项目,都给出了错误的答案,每个建议只是添加了更多错误的答案。

答案实际上非常简单,您可能会想破脑袋:只需将文件扩展名从“.html”更改为“.php”!请记住,您可以完全使用 PHP 构建一个网页,所有 JavaScript 以及基于 JavaScript 构建的内容(例如 JQuery、bootstrap 等)都可以工作。

这是一个简单的证明示例:

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Blank Web Page</title>

    <link rel="stylesheet" type="text/css" href="css/css.css">

</head>

<body>

    <?php

    $son = 5;
    $nos =10;

    echo $son + $nos;

    ?>

    <h4>test to see if this html element can be output too!</h4>

    <script type="text/javascript" src="js/js.js"></script>

</body>

请注意,我正在使用您的标准 html,即使它没有显示我的 HTML 标签(相信我,它就在那里)、网页内容并在其中插入了 php 代码。当然结果是 15 并且 html 元素 h4 也正确渲染。将扩展名改回“html”,您将仅获得 h4 元素,并且您会发现您的 php 代码已使用 html 的多注释被注释掉。

我忘了补充一点,这也适用于 Xampp。

Too much overkill. All these suggestions lead me down the wrong path for like 5 hours. JK, but I did read a lot of google search items all giving wrong answers and each suggestion was just adding more wrong answers.

The answer is in fact so simple you would want to bang your head: Simply change the file extension from ".html" to ".php"!!! Remember that you can build a webpage entirely out of PHP and all JavaScript and stuff built off JavaScript like, JQuery, bootstrap, etc will work.

Here is a simple example of proof:

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Blank Web Page</title>

    <link rel="stylesheet" type="text/css" href="css/css.css">

</head>

<body>

    <?php

    $son = 5;
    $nos =10;

    echo $son + $nos;

    ?>

    <h4>test to see if this html element can be output too!</h4>

    <script type="text/javascript" src="js/js.js"></script>

</body>

Notice that I am using your standard html, even though it doesn't show my HTML tags(trust me it's there), web page stuff and have php code inserted inside. Of course the result is 15 and the html element h4 renders correctly too. Change the extension back to "html" and you will get only the h4 element and you will find that your php code has been commented out using multi-comment for html.

I forgot to add that this works for Xampp too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文