Are there differences between /, /* */ and #?

Asked

Viewed 363 times

13

Considering how important it is to comment on a code, I came up with this question.


I realized there are several ways to comment lines/parts of code:

  • //
  • /* */
  • #

Example:

// comentando uma linha

/* comentando
   um intervalo
   do código */

# comentando uma linha

Doubts:

  • There are other ways to comment?
  • Taking these definitions from the example, there are more differences between them?
  • 3

    Downvoter, how can I improve the question for you?

  • Varies in language, in the Delphi for example block comet we use {}. The differences you yourself have scored on your question.

  • Good @Rbz, hadn’t stopped to think if there are differences between type comments

  • @Robertodecampos My main question is between // and #... Is it not to "disturb" a break case? Example: \/n... Hey, thanks a Wees!

  • 3

    OBS: I wasn’t the one who negatived it. \\, # or between /**/ will be interpreted by the compiler unless it is within a string. So \\/n will change nothing in your code, #/n neither.

  • I think the "difference" is the source, // is c++ style; # is shell style, but that’s it.

  • 3

    I think the question is only about PHP, we don’t even have to talk about C or Delphi, also I don’t understand why downvotes, I see this question as an excellent way to help beginner programmers and make it all a great source of research.

  • To sql is used --

Show 3 more comments

1 answer

17


The essential thing is this:

  • The // and the # are identical in terms of operation;

  • The // and the /* are completely different in terms of operation, and each has a specific use scenario:

    The // or # are for line comments. Everything that comes after them is ignored, until you find a line break.

    Already, the /* is a block comment which can end: 1) before the end of the line; or 2) different line from where it started. In both cases what determines the end of the comment is the sequence */;

  • There is a third situation, the __halt_compiler, that is not comment, but it also causes the code to be ignored from that point on (it can be used to "ignore" a longer comment, or even have a special function to reuse the source file).

Otherwise, no differences at all.


Notes:

  • You can’t nest block comments.

    /* abc /* 123 */ def */
    

    in the above case, def is no longer commented, will give error. The first */ it is worth. (The fact is that the second /* is ignored).

  • There’s a technique for "keying" very curious block comments, which is this one (credits to the PHP comment section):

    When you define a block like this, just modify a bar to "count" it or not:

    //*
        Bloco "chaveável" com várias linhas.
        Em princípio, são duas linhas com "comment de linha"
        Basta modificar UMA barra, que vira um "comment de bloco"
    // */
    

    Except for the first bar:

    /*
        Bloco "chaveável" com várias linhas.
        Em princípio, são duas linhas com "comment de linha"
        Basta modificar UMA barra, que vira um "comment de bloco"
    // */
    
  • It has a type of comment that is used in documentation tools (not part of the language itself) which is the /**. In practice is a normal block comment /* followed by an asterisk (although, a special token was adopted for this case in the parser, the T_DOC_COMMENT).

    For more details, see this post:

    What’s the difference between "/*" and "/**" comments in PHP?

  • Is there any difference in the compiler’s comment type performance? Like the difference between 'nome: ' . $nome and "nome: {$nome}"... Thinking about the part of the comment block the compiler would have to find the beginning and the end, costing a little more - right, or is irrelevant?

  • 2

    @Papacharlie in any case the job is the same, only changes the end marker, which in one case is \n and the other is */ - What really influences is the amount of characters in the comment, but even so is negligible (if the person is using PHP and not a compiled language, probably these details are irrelevant - the time difference is derisory and the application)

  • It wouldn’t be interesting to point out that no comment type overrides the closing tag ?>

  • @Leocaracciolo I will give a confirmed if it is valid for all versions, and update later (with due credit)

  • 1

    Give unto Caesar that which is Caesar’s, and unto God that which is God’s!! Credito é de @rray veja nessa resposta https://answall.com/questions/94374/qual-a-diferen%C3%A7a-entre-tipos-de-coment%C3%A1rios-em-php/94383#94383

  • @Leocaracciolo Very interesting and valid about the /** !

  • 1

    Very good this multi-line lockable block business. I will use a lot from now on!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.