PHP在HTML中被注释掉 [英] PHP gets commented out in HTML

查看:88
本文介绍了PHP在HTML中被注释掉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

我做错了什么?为什么将其注释掉?

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

解决方案

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

因此,由于PHP标记在HTML文件中无效,因此如果未对服务器进行预处理,浏览器将无法识别它,因此它会自动将其转换为注释,因为它不知道该怎么做. /p>

编辑以获取其他信息:

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

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

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

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

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

<!--?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.

解决方案

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.

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.

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.

Note that even if you have a server running, opening the .php file 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在HTML中被注释掉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆