Is there any use in using PHP comments over HTML comments in HTML?

I heard that someone said that there is, so I was interested.

HTML comment:

<!-- Comment goes here. --> 

PHP comment:

 <?php // Comment goes here. ?> 
+43
html comments php
Aug 09 2018-11-11T00:
source share
6 answers

Unlike HTML comments, PHP comments are not displayed in the final release. This is often desirable since comments are usually internal notes that are not a single business.

+111
Aug 09 2018-11-11T00:
source share

PHP comments will not be displayed in the source on the client, where there will be HTML comments. So the question is: do you want the comment to be read by the end user?

+25
Aug 09 2018-11-11T00:
source share

PHP comments are not displayed in the output HTML, as other users have stated. This has 2 main effects:

  • PHP comments are hidden from the end user. This has already been considered ... And,
  • PHP comments are not sent over the Internet. This will lead to some increase in performance for the end user. The advantage of this, of course, depends on your verbosity.
+10
Aug 09 '11 at 12:48
source share

Small HTML comments can be useful for front-end developers, for example, specifying the identifier of the closing tag in large files:

 <div id="container"> ... Hundreds or thousands of lines of HTML </div> <!-- #container --> 

These comments can be extremely useful and have little or no effect on page size.

PHP comments should be kept to a minimum in your displayed code, because there should not be a lot of PHP for comments (although this is a completely different subject).

It goes without saying that you should only comment on PHP with PHP comments and HTML with HTML comments. If you ever have to write long HTML comments to explain some dodgy HTML files to your developers, this is probably a bad sign.

+3
Aug 15 2018-11-11T00:
source share

Really depends on what you are commenting on. In addition to all the other answers, I have something that can really make a difference. From time to time, we programmers would have to comment on a block of code, for example:

 <?php foreach ($results) { ?> <div> ... more divs ... </div> <?php } ?> 

Suppose the length of $results is 20, and the length of the characters of each div is about 500 bytes (moderately dense div). Then we have 10 KB of HTML. In this case, the two comment methods will be very different:

 <?php /* foreach ($results) { ?> <div> ... more divs ... </div> <?php } */ ?> 

Zero bytes are sent to the visitor, and there is no PHP processing.

against

 <!-- <?php foreach ($results) { ?> <div> ... more divs ... </div> <?php } ?> --> 

10 KB is still sent to the visitor, and PHP starts this loop with a huge y loop for nothing.

Of course, if you use a version control system (for example, git, svn), such comments (comments that cover the current code, not descriptions) should really be deleted at all.

+2
Nov 24 2018-11-11T00:
source share

Sometimes html comments are useful when you need to get information that the browser cannot display in html.

For example, you need to know the IP address of the server that sent the html page that you cannot show on the html page .. you print it as a html comment so that you can check this information in the HTML source

+1
Aug 09 '11 at 12:51 on
source share



All Articles