PHP注释标签


PHP Comment Tag

在Java/JSP的美妙世界中,您可以使用这种形式的注释:

<%-- anything in here is ignored and does not get sent to the client 
     it can span multiple lines and is useful for commenting out blocks
     of JSP, including tags and HTML:
     <c:if test="${some.condition}">
       <p>All this is inside the comment</p>
     </c:if>
     <!-- this HTML comment is itself commented out of the JSP so will not be sent to the client --!>
--%>
<!-- but this is just an HTML comment and WILL be sent to the client -->

在PHP这个不那么美妙的世界里,我能找到的唯一参考注释是:

/*
  multi-line comment
*/
这些:

// single line comment

但是这些不会注释掉HTML和PHP标签:

/*
 <? do_something() ?>
*/

导致/*和*/被呈现给浏览器,并且do_something()仍然被调用。

在PHP中是否有类似于上面所示的JSP注释?

这不会注释掉一个块的原因:

/*
 <? do_something() ?>
*/

表示您不是在php中而是在html中,并且/* */在html中不是有效的注释结构。

如果你有

<?php
/*
some_php();
?>
and html
<?php
more_php();
*/
?>

它会工作得很好。注释块中的php代码不会被执行,也不会被发送到浏览器。

虽然它在so代码高亮上工作得不太好…

只要确保你是在php中(在<?php标签之后)当你打开你的评论区

你需要明白你在同一个文件中编写HTML和PHP,它们都由它们自己的处理器(浏览器和服务器)执行。

<?php ?>标签之间的代码被认为是PHP,并通过服务器处理。外部的所有内容都将返回到浏览器进行处理(被视为HTML)。

因为,// or //是php注释块,不是超文本标记语言的一部分,它们不会在输出的HTML中工作,相反,如果在适当的标记之外发现,它们将作为字符出现在网页上。

在同样的意义上,HTML注释代码 <!-- -->不会在php中充当注释,如果您试图在php标记中使用它们,则很可能导致错误。