PHP 在 HTML 中被注释掉

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21279901/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:59:32  来源:igfitidea点击:

PHP gets commented out in HTML

phphtml

提问by Renier

I created a simple HTML webpage that includes the following PHP code in the HTML code.

我创建了一个简单的 HTML 网页,在 HTML 代码中包含以下 PHP 代码。

<?php echo date('l, F jS, Y'); ?>

When I run the HTML page and I view the source code it shows:

当我运行 HTML 页面并查看它显示的源代码时:

<!--?php echo date('l, F jS, Y'); ?-->

What am I doing wrong? Why is it being commented out?

我究竟做错了什么?为什么会被注释掉?

The HTML webpage file has the extension .html.

HTML 网页文件的扩展名为 .html。

回答by Joeytje50

To run PHP scripts, you have to save the file as a .phpfile. You will also need to execute it on a server. You can't run php directly from your browser, since PHP is a HTML preprocessor - your browser has nothing to do with PHP, it only gets the HTML that is generated by the server.

要运行 PHP 脚本,您必须将文件另存为.php文件。您还需要在服务器上执行它。您不能直接从浏览器运行 php,因为 PHP 是一个 HTML 预处理器 - 您的浏览器与 PHP 无关,它只获取服务器生成的 HTML。

So because PHP tags are not valid in HTML files, when not preprocessed by the server, the browser doesn't recognise it, so it automatically converts it to comments since it doesn't know what else to do with it.

所以因为 PHP 标签在 HTML 文件中是无效的,当没有被服务器预处理时,浏览器不会识别它,所以它会自动将其转换为注释,因为它不知道还能用它做什么。

Edit for additional information:

编辑其他信息

If you want to see the result of the processed php file, you'll need to run it from some kind of server (using Apache through XAMPP for example, but there's loads of options). If you then view the resulting page on the localhost server, it'll give you the processed php code, which should be the desired output. You can check the manual for whichever program you're using to run your server for more details on how to do this.

如果您想查看处理后的 php 文件的结果,您需要从某种服务器运行它(例如,通过 XAMPP 使用 Apache,但有很多选项)。如果您随后在 localhost 服务器上查看结果页面,它将为您提供已处理的 php 代码,这应该是所需的输出。您可以查看用于运行服务器的任何程序的手册,以获取有关如何执行此操作的更多详细信息。

Note that even if you have a server running, opening the .phpfile locally in your browser still doesn't show you the processed result (ie. it will still show your php code as comments). You need to view the page through something like http://localhost/mypage.php, or whichever location is set as default url for your specific localhost server.

请注意,即使您有一台正在运行的服务器,.php在浏览器中本地打开文件仍然不会向您显示处理结果(即它仍然会将您的 php 代码显示为注释)。您需要通过类似http://localhost/mypage.php, 或任何设置为特定本地主机服务器的默认 url 的位置来查看页面。

回答by CramerTV

What am I doing wrong?

我究竟做错了什么?

If the file is being served by Apache, you have not enabled the php interpreter to run on html files. Apache (by default) does not run the php interpreter on html files.

如果文件由 Apache 提供,则您尚未启用 php 解释器以在 html 文件上运行。Apache(默认情况下)不会在 html 文件上运行 php 解释器。

why is it being commented out?

为什么会被注释掉?

As others have noted, a browser does not know what to do with the php tag.

正如其他人所指出的,浏览器不知道如何处理 php 标签。





If you want to interpret php in html files instead of renaming the files to .php then you can to add the .html extension to the php interpreter as below:

如果您想在 html 文件中解释 php 而不是将文件重命名为 .php 那么您可以将 .html 扩展名添加到 php 解释器中,如下所示:

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

This line is in the httpd.conf file.

这一行在 httpd.conf 文件中。

I'm not suggesting this is a proper way to do it but I believe it does answer your first question.

我并不是说这是一个正确的方法,但我相信它确实回答了你的第一个问题。

回答by crazy_abdul

In my case it was because I had a syntax error with my php code. I typed

就我而言,这是因为我的 php 代码有语法错误。我打字

<? php
    phpinfo();
?>

Instead of

代替

<?php
    phpinfo();
?>

The space between <?and phpcaused the headache for me

<?和之间的空间php让我头疼

回答by user2072826

I had the same problem and fixed it by changing the URL from file://www/[Project] to localhost/[Project].

我遇到了同样的问题,并通过将 URL 从 file://www/[Project] 更改为 localhost/[Project] 来修复它。

Even if the file is saved as a .php, it will comment out the PHP if the file is opened from the file system.

即使文件保存为 .php,如果从文件系统打开文件,它也会注释掉 PHP。

回答by Kaushik

Reference

参考

Create a file named hello.php and put it in your web server's root directory (DOCUMENT_ROOT) with the following content:

创建一个名为 hello.php 的文件并将其放入您的 Web 服务器的根目录 (DOCUMENT_ROOT) 中,内容如下:

Example #1 Our first PHP script: hello.php

Example #1 我们的第一个 PHP 脚本:hello.php

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>

Use your browser to access the file with your web server's URL, ending with the /hello.php file reference. When developing locally this URL will be something like http://localhost/hello.phpor http://127.0.0.1/hello.phpbut this depends on the web server's configuration. If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser:

使用您的浏览器通过您的 Web 服务器的 URL 访问该文件,以 /hello.php 文件引用结尾。在本地开发时,此 URL 将类似于http://localhost/hello.php或,http://127.0.0.1/hello.php但这取决于 Web 服务器的配置。如果一切配置正确,PHP 将解析此文件,并将以下输出发送到您的浏览器:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p>
 </body>
</html>

回答by Vivek Pandey

Since you can't run PHP directly in the browser, you need to have a server and at the same time, you need to have a .php file in order to execute PHP script.

由于无法在浏览器中直接运行 PHP,因此需要有服务器,同时还需要有 .php 文件才能执行 PHP 脚本。