【问题标题】:PHP Comment TagPHP 注释标签
【发布时间】:2015-08-25 22:20:34
【问题描述】:

在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 世界中,我能找到的唯一对 cme​​ts 的引用是:

/*
  multi-line comment
*/

还有这些:

// single line comment

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

/*
 <? do_something() ?>
*/

导致 /* 和 */ 被渲染到浏览器,并且 do_something() 仍然被调用。

在 PHP 中是否存在与上面显示的 JSP 注释等效的内容?

【问题讨论】:

  • 为了完成,再来一个:# This is also a php comment

标签: php tags comments


【解决方案1】:

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

&lt;?php ?&gt; 标签之间的代码被认为是 PHP,并通过服务器处理。 外部的所有其他内容基本上都回显给浏览器进行处理(被视为 HTML)。

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

同样,HTML 注释代码 &lt;!-- --&gt; 不会充当 php 中的 cmets,如果您尝试在 php 标签中使用它们,很可能会导致错误。

【讨论】:

    【解决方案2】:

    this不会注释掉一个block的原因:

    /*
     <? do_something() ?>
    */
    

    只是你不在php中,而是在html中,/* */在html中不是有效的注释结构。

    如果你有

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

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

    虽然它在 SO 代码荧光笔上效果不佳...

    当您打开评论部分时,请确保您在 php 中(在 &lt;?php 标记之后)。

    【讨论】:

    • 在你的情况下,我猜 'and html' 将在页面上呈现,换句话说,没有等同于注释掉所有内容,对吗?
    • @NickJ 不,评论部分中的任何内容都不会发送到浏览器。 php 在服务器上得到处理,只有最终结果被发送到浏览器。例如:codepad.viper-7.com/dtFnQx
    • 是的,你是对的, /* ?> 和 */ ?> 似乎等同于 JSP 的 。谢谢
    猜你喜欢
    • 2015-12-19
    • 2018-10-25
    • 2020-10-10
    • 2014-12-03
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 2012-10-01
    相关资源
    最近更新 更多