Php在html中被注释掉了。

时间:2021-03-15 01:14:15

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?

我究竟做错了什么?为什么要被评论出来?

5 个解决方案

#1


29  

To run PHP scripts, you have to save the file as a .php file. 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文件中无效,因此当服务器未对其进行预处理时,浏览器无法识别它,因此它会自动将其转换为注释,因为它不知道还有什么用它做。

#2


11  

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.

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

#3


4  

I know this is kinda late, but I had the same problem and fixed it by changing the url from file://www/[Project] to localhost/[Project].

我知道这有点晚了,但我遇到了同样的问题并通过将文件从// 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。

#4


3  

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

示例#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.php or http://127.0.0.1/hello.php but 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>

#5


0  

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脚本。

#1


29  

To run PHP scripts, you have to save the file as a .php file. 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文件中无效,因此当服务器未对其进行预处理时,浏览器无法识别它,因此它会自动将其转换为注释,因为它不知道还有什么用它做。

#2


11  

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.

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

#3


4  

I know this is kinda late, but I had the same problem and fixed it by changing the url from file://www/[Project] to localhost/[Project].

我知道这有点晚了,但我遇到了同样的问题并通过将文件从// 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。

#4


3  

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

示例#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.php or http://127.0.0.1/hello.php but 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>

#5


0  

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脚本。