一起注释 HTML 和 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>

我很乐意在一个镜头里评论这两个... 但是当我尝试

    <!-- <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 代码没有被注释掉... 有没有办法做到这一点?

111620 次浏览

Instead of using HTML comments (which have no effect on PHP code -- which will still be executed), you should use 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>
*/ ?>


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.


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

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

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

You can only accomplish this with PHP comments.

 <!-- <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


/*


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;


*/
?>

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 //).

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:

<?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 //* ?>

You can also use this as a comment:

<?php
/* get_sidebar(); */


?>

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

Wrap all the content in this:

<?php
if(false){
?>


Here goes your PHP + HTML code


<?php
}
?>