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

12

Considering how important it is to comment on a code , that question came to me.

I noticed that there are several ways to comment lines / parts of the code :

  • //
  • /* */
  • #

Example:

// comentando uma linha

/* comentando
   um intervalo
   do código */

# comentando uma linha

Questions:

  • Are there other ways to comment?
  • Excluding these settings from the example, are there more differences between them ?
asked by anonymous 08.08.2018 / 21:54

1 answer

17

The essential thing is this:

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

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

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

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

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

No more differences.


Notes:

  • You can not nest block comments.

    /* abc /* 123 */ def */
    

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

  • It has a technique to "key" comments of block very curious, that is this (credits to the comment section of PHP):

    When you define a block like this, simply modify a slash so that it is "worth" 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"
    // */
    

    Excluding only the first slash:

    /*
        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 /** . In practice it is a regular block comment /* followed by an asterisk (although, a special token for that case was adopted in parser , T_DOC_COMMENT ).

    For more details, see this post:

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

    p>
08.08.2018 / 23:02