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 个标签。