一起注释掉 HTML 和 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5781375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:28:28  来源:igfitidea点击:

Comment out HTML and PHP together

phphtml

提问by Matt Elhotiby

I have this code,

我有这个代码

    <tr>
      <td><?php echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
    </tr>

and I would love to comment both in one shot...but when I try

我很想一口气发表评论……但是当我尝试时

    <!-- <tr>
      <td><?php echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
    </tr> -->

the page fails - it seems the PHP code is not being commented out... Is there a way to do this?

页面失败 - 似乎 PHP 代码没有被注释掉......有没有办法做到这一点?

回答by Pascal MARTIN

Instead of using HTML comments (which have no effect on PHP code -- which will still be executed), you should use PHP comments:

您应该使用 PHP 注释,而不是使用 HTML 注释(这对 PHP 代码没有影响——仍将被执行)

<?php /*
<tr>
      <td><?php echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
    </tr>
*/ ?>


With that, the PHP code inside the HTML will not be executed; and nothing (not the HTML, not the PHP, not the result of its non-execution)will be displayed.


这样,HTML 中的 PHP 代码将不会被执行;并且不会显示任何内容(不是 HTML,不是 PHP,不是其未执行的结果)


Just one note: you cannot nest C-style comments... which means the comment will end at the first */encountered.


请注意:您不能嵌套 C 风格的注释……这意味着注释将在第一次*/遇到时结束。

回答by Nev Stokes

I agree that Pascal's solution is the way to go, but for those saying that it adds an extra task to remove the comments, you can use the following comment style trick to simplify your life:

我同意 Pascal 的解决方案是可行的方法,但是对于那些说它增加了删除评论的额外任务的人,您可以使用以下评论样式技巧来简化您的生活:

<?php /* ?>
<tr>
      <td><?php echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
    </tr>
<?php // */ ?>

In order to stop the code block being commented out, simply change the opening comment to:

为了阻止代码块被注释掉,只需将开始注释更改为:

<?php //* ?>

回答by nick

I found the following solution pretty effective if you need to comment a lot of nested HTML + PHP code.

如果您需要注释大量嵌套的 HTML + PHP 代码,我发现以下解决方案非常有效。

Wrap all the content in this:

将所有内容包裹在此:

<?php
    if(false){
?>

Here goes your PHP + HTML code

<?php
    }
?>

回答by Dammy Akinsiku

The <!-- -->is only for HTML commenting and the PHP will still run anyway...

<!-- -->只对HTML注释和PHP仍然想运行...

Therefore the best thing I would do is also to comment out the PHP...

因此,我会做的最好的事情也是注释掉 PHP ...

回答by Flipper

You can only accomplish this with PHP comments.

您只能使用 PHP 注释来完成此操作。

 <!-- <tr>
      <td><?php //echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php //echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php //echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php //echo $sort_order; ?>" size="1" /></td>
    </tr> -->

The way that PHP and HTML works, it is not able to comment in one swoop unless you do:

PHP 和 HTML 的工作方式,除非你这样做,否则它无法一口气评论:

<?php

/*

echo <<<ENDHTML
 <tr>
          <td>{$entry_keyword}</td>
          <td><input type="text" name="keyword" value="{echo $keyword}" /></td>
        </tr>
        <tr>
          <td>{$entry_sort_order}</td>
          <td><input name="sort_order" value="{$sort_order}" size="1" /></td>
        </tr>
ENDHTML;

*/
?>

回答by Ehsan Khodarahmi

PHP parser will search your entire code for <?php(or <?if short_open_tag = On), so HTML comment tags have no effect on PHP parser behavior & if you don't want to parse your PHP code, you have to use PHP commenting directives(/* */or //).

PHP 解析器将搜索您的整个代码<?php(或者<?如果 short_open_tag = On),因此 HTML 注释标签对 PHP 解析器行为没有影响,如果您不想解析 PHP 代码,则必须使用 PHP 注释指令(/* *///) .

回答by Mark

You can also use this as a comment:

您也可以将其用作评论:

<?php
    /* get_sidebar(); */

?>