PHP Comment Tag

1.8k views Asked by At

In the wonderful world of Java/JSP, you can use this form of commenting:

<%-- anything in here is ignored and does not get sent to the client 
     it can span multiple lines and is useful for commenting out blocks
     of JSP, including tags and HTML:
     <c:if test="${some.condition}">
       <p>All this is inside the comment</p>
     </c:if>
     <!-- this HTML comment is itself commented out of the JSP so will not be sent to the client --!>
--%>
<!-- but this is just an HTML comment and WILL be sent to the client -->

In in the much less wonderful world of PHP, the only reference to comments I can find are these:

/*
  multi-line comment
*/

and these:

// single line comment

But these WON'T comment out chucks of HTML and PHP tags:

/*
 <? do_something() ?>
*/

results in /* and */ being rendered to the browser, and do_something() is still called.

Is there an equivalent of the JSP comment shown above in PHP?

2

There are 2 answers

3
jeroen On BEST ANSWER

The reason that this will not comment out a block:

/*
 <? do_something() ?>
*/

is simply that you are not in php but in html and /* */ is not a valid comment structure in html.

If you had

<?php
/*
some_php();
?>
and html
<?php
more_php();
*/
?>

it would work just fine. The php inside the comment block will not be executed and nothing of it will get sent to the browser.

Although it doesn't work so well on the SO code highlighter...

Just make sure you are in php (after a <?php tag) when you open your comment section.

0
LoneWolf47 On

You need to understand that you're writing HTML and PHP in same file, both to be executed by they're own processers ( browser and server ).

The code between the <?php ?> tags is considered PHP and is processed via the server. Everything else outside is essentially echoed to the browser to process ( to be considered as HTML ).

Since , // or // are php comment blocks and are not part of the hypertext markup language they will not work in the outputted HTML and instead will appear as characters on the webpage if discovered outside of the appropriate tags.

In the same sense that the HTML comment code <!-- --> won't act as comments in php and will most likely result in errors if you attempted to use them within the php tags.