Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a hash sign (#) for commenting in PHP?

Tags:

comments

php

People also ask

How do I use the hash symbol?

Also called a hash, number sign, or pound sign, the octothorpe is the typographical symbol "#" (two horizontal lines and two vertical lines, crossed). On US QWERTY keyboards, the # symbol appears on the same key as the number 3. It can be typed by holding Shift and pressing the 3 key.

What does the hash (#) sign mean?

The symbol # is known variously in English-speaking regions as the number sign, hash, or pound sign. The symbol has historically been used for a wide range of purposes including the designation of an ordinal number and as a ligatured abbreviation for pounds avoirdupois – having been derived from the now-rare ℔.

Is it a pound symbol or hashtag?

The # symbol is commonly called the pound sign, number sign and more recently the hashtag. It is called the pound sign because the symbol comes from the abbreviation for weight, lb, or “libra pondo” literally “pound by weight” in Latin.

Why do I get a pound sign instead of a hashtag?

Your computer is likely setup for multiple keyboard layouts and a hotkey has been inadvertently pressed to switch between layouts. Modify or delete the hotkeys for switching between keyboard layouts by selecting an option and clicking Change Key Sequence.


2021 UPDATE: As of PHP 8, the two characters are not the same. The sequence #[ is used for Attributes.(Thanks to i336 for the comment)

Original Answer:

The answer to the question Is there any difference between using "#" and "//" for single-line comments in PHP? is no.

There is no difference. By looking at the parsing part of PHP source code, both "#" and "//" are handled by the same code and therefore have the exact same behavior.


PHP's documentation describes the different possibilities of comments. See http://www.php.net/manual/en/language.basic-syntax.comments.php

But it does not say anything about differences between "//" and "#". So there should not be a technical difference. PHP uses C syntax, so I think that is the reason why most of the programmers are using the C-style comments '//'.


<?php
    echo 'This is a test'; // This is a one-line C++ style comment
    /* This is a multi-line comment.
       Yet another line of comment. */
    echo 'This is yet another test.';
    echo 'One Final Test'; # This is a one-line shell-style comment
?>

RTM


Is there any reason, aside from personal preference, to use // rather than # for comments?

I think it is just a personal preference only. There is no difference between // and #. I personally use # for one-line comment, // for commenting out code and /** */ for block comment.

<?php
    # This is a one-line comment
    echo 'This is a test';

    // echo 'This is yet another test'; // commenting code

    /** 
     * This is a block comment
     * with multi-lines 
     */
    echo 'One final test';
?>