Reputation: 44066
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?
Upvotes: 113
Views: 120833
Reputation: 3239
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
}
?>
Upvotes: 16
Reputation: 59
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...
Upvotes: 4
Reputation: 400952
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>
*/ ?>
Upvotes: 214
Reputation: 9769
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 //* ?>
Upvotes: 47
Reputation: 4922
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 //
).
Upvotes: 1
Reputation: 2609
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;
*/
?>
Upvotes: 2