110

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321

7 Answers7

207

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • This does add a little bit more (you need the PHP tags as well as the PHP comment markup), but this is the best way to do this, I agree. – qJake Apr 25 '11 at 18:01
  • But then when he uncomments he has to remove the taags as well which is a little more work. – Flipper Apr 25 '11 at 18:01
  • 5
    @flipper: so? it's an extra few characters v.s. having to rip out every block of PHP code by hand. – Marc B Apr 25 '11 at 18:17
  • Interestingly, it seems that NetBeans 8.0.2 doesn't understand this (and so formats my code incorrectly even though this is a valid way to comment out HTML). – Ryan Feb 27 '16 at 02:18
  • whats the shortcut to do it in sublime text editor? I know ctrl+c but that doesn't do PHP comments. – ramya Aug 29 '17 at 16:25
  • This ways seems quite unsafe, since there's a strong risk of it breaking out midway through because you used comments within the commented-out section. I think it's better just to use an `if (false) {` block here. – mwfearnley Feb 25 '22 at 13:57
47

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 //* ?>
Nev Stokes
  • 9,051
  • 5
  • 42
  • 44
16

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
    }
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nick
  • 2,819
  • 5
  • 33
  • 69
  • 1
    This is the only solution that _actually works_, I don't understand why this isn't the top answer :) Thanks a lot, dude! – bviktor Sep 05 '18 at 17:01
  • For the 3 people who actually mean this question the way it's asked: THIS ^ is your answer ;) - Note: I recommend the if(false): - endif; syntax, as it may be more obvious to the next dev that it is meant to affect the markup – MJHd Mar 31 '20 at 22:02
4

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

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;

*/
?>
Flipper
  • 2,589
  • 3
  • 24
  • 32
0

You can also use this as a comment:

<?php
    /* get_sidebar(); */

?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark
  • 21
  • 3
0

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

Ehsan Khodarahmi
  • 4,772
  • 10
  • 60
  • 87