简体   繁体   English

为什么某些 PHP 代码呈现为 HTML 注释

[英]Why does some PHP code render as HTML comments

I just started learning PHP and installed webmatrix to get started.我刚开始学习 PHP 并安装了 webmatrix 开始使用。 My first test page renders some of the PHP code as HTML comments.我的第一个测试页面将一些 PHP 代码呈现为 HTML 注释。 I need help determining what's wrong.我需要帮助确定出了什么问题。 One include statement works fine while another in the same file outputs as seen below.一个 include 语句工作正常,而另一个在同一个文件中的输出如下所示。 None of the PHP code withing HTML displays.没有任何带有 HTML 的 PHP 代码显示。 The file contains HTML and PHP code and is named with a .PHP extension.该文件包含 HTML 和 PHP 代码,并以 .PHP 扩展名命名。 I do have PHP enabled in the site settings.我确实在站点设置中启用了 PHP。

For example this line:例如这一行:

<?
include "phpinfo.php";    
?>

becomes:变成:

<!--? include "phpinfo.php"; ?-->

05/11: Thanks for all the help. 05/11:感谢所有帮助。 I got it working and learned a few things.我让它工作并学到了一些东西。

You should ideally be using <?php to open and ?> to close php statements to be evaluated.理想情况下,您应该使用<?php来打开和?>来关闭要评估的 php 语句。 You can use short-tags <?= and ?> if you just want to print a value.如果您只想打印一个值,您可以使用短标签<?=?>

Either way it looks to me like php isn't being rendered by your web server.无论哪种方式,在我看来,您的 Web 服务器都没有呈现 php。 When the tag <?当标签<? is received by the browser, the browser treats it as something which shouldn't be rendered to the end user (which indeed it shouldn't - it should be getting evaluated by your server prior to being sent to the client).被浏览器接收,浏览器将其视为不应该呈现给最终用户的东西(实际上它不应该 - 它应该在发送到客户端之前由您的服务器进行评估)。

try the following line in your php file:在您的 php 文件中尝试以下行:

<?php phpinfo() ?>

If this doesn't work then you need to check if php is actually running on your server.如果这不起作用,那么您需要检查 php 是否真的在您的服务器上运行。

If it does work then as Darren commented below it is likely that you do not have short-tags set to allowed in your php.ini file - see SO answer here如果它确实有效,那么正如 Darren 在下面评论的那样,您的php.ini文件中可能没有将短标签设置为允许 - 请参阅此处的答案

Check your file extension, it should be a .php file, not a .html file.检查您的文件扩展名,它应该是.php文件,而不是.html文件。

Hopefully this solves your problem.希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM