Comment HTML and PHP together

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 like to comment both in one frame ... 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 that the PHP code is not commented out ... Is there a way to do this?

+80
html php
Apr 25 2018-11-17T00:
source share
7 answers

Instead of using HTML comments (which do not affect the PHP code - which will still execute), 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> */ ?> 


However, PHP code inside HTML will not execute; and nothing (neither HTML, nor PHP, nor the result of non-execution) will be displayed.


Just one note: you cannot nest comments in C style ... this means that the comment ends on the first */ met.

+163
Apr 25 '11 at 18:00
source share

I agree that Pascal's solution is the way to go, but for those who say they add an extra task to remove comments, you can use the following comment style trick to make your life easier:

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

To stop code blocking, just change the opening comment:

 <?php //* ?> 
+39
Apr 25 '11 at 19:35
source share

<!-- --> only for HTML comments, and PHP will still work ...

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

+4
Apr 25 '11 at 18:01
source share

I found the following solution quite effective if you need to comment on a lot of embedded HTML + PHP code.

Wrap all the contents in this:

 <?php if(false){ ?> Here goes your PHP + HTML code <?php } ?> 
+4
Apr 02 '18 at 21:58
source share

You can only do 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> --> 

How PHP and HTML work, it cannot comment in one fell 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; */ ?> 
+2
Apr 25 '11 at 18:01
source share

The PHP parser will look for all the code for <?php (or <? If short_open_tag = On), so the HTML comment tags do not affect the behavior of the PHP parser, and if you do not want to parse your PHP code, you should use directives for PHP comments ( /* */ or // ).

0
Apr 25 2018-11-21T00:
source share

You can also use this as a comment:

 <?php /*get_sidebar();*/ ?> 
0
Jul 19 '17 at 15:28
source share



All Articles