5
<?php
    # some comment
 ?>

and

<?php 
   // some comment
?>

are used for single-line code commenting in PHP, and that former comes from shell scripting and // comes from C++.

However I am curious to know if there are any differences between using // and # for single line commenting and anyone has come across cases specific cases where one or other should not be used.

Only difference I could think of is there is one character in '#' and two in '//' so perhaps will there in larger scripts some small size and/or performance gains ??

Manquer
  • 7,390
  • 8
  • 42
  • 69
  • 3
    No difference. It's not worth micro-optimizing a single character. – Jessica Jun 12 '13 at 15:33
  • size gains would be very limited, even in very large application. regarding performance, I don't think comments have an impact on performance at all as they should just be "omitted" at execution time – Laurent S. Jun 12 '13 at 15:35
  • 1
    I believe this explains it somewhat: http://php.net/manual/en/language.basic-syntax.comments.php – lurker Jun 12 '13 at 15:36
  • 1
    Unlike Life, in programming we always have alternates to do the same thing so just enjoy it rather over pondering it :) – wakqasahmed Jun 12 '13 at 15:36
  • Thats what only reason i could think of, which sounded pretty lame even to me .. – Manquer Jun 12 '13 at 15:37
  • @wakqasahmed ofcourse :) part of the fun is the pondering part though :P – Manquer Jun 12 '13 at 15:41
  • 1
    For me it's easier to type `//` by double pressing the key on my keyboard moving just my right pinky one key down and pressing it two times. If I want to do `#` I need to use both hands and the movements are "bigger" ;). It's the same for `echo` and `print`. But in print and echo "scenario" you can hear an argument that one function is a little slower, however I am not sure right now which one ;) but it's really something that is no deal-breaker when optimizing for code I guess. – Derfder Jun 12 '13 at 15:42
  • 1
    @Bartdude size and performance gains are lame in today's context yeah i noe.. i thought perhaps in some cases you should not use one or the other like how when using multi-line comment with /* */ regexes have to kept in mind as illustrated here http://www.php.net/manual/en/language.basic-syntax.comments.php#48204 – Manquer Jun 12 '13 at 15:44
  • @Derfder thats an interesting take! if you had submitted that as an answer i would have selected it :) – Manquer Jun 12 '13 at 15:46
  • @Manquer : size and perf optimization are not lame, but in this specific case the size gain will be so limited it is indeed useless, and there won't be any perf gain. Of course if for some reason the way you comment causes parsing error, you obviously have to find another way... – Laurent S. Jun 12 '13 at 15:47
  • @Manquer Thanks, I have added it as an answer. – Derfder Jun 12 '13 at 15:50
  • 1
    Btw. why the downvotes? In my opinion it's an interesting question. – Derfder Jun 12 '13 at 15:53
  • @Bartdude fair enough... and i do think outside of these reasons there is definite reduction in complexity while writing parsers of source code(like in doc generators) if only # is used, // will probably be slightly more difficult to handle – Manquer Jun 12 '13 at 15:56

1 Answers1

3

For me it's easier to type // by double pressing the key on my keyboard moving just my right pinky one key down and pressing it two times.

If I want to do # I need to use both hands and the movements are "bigger" ;). It's the same for echo and print.

But in print and echo "scenario" you can hear an argument that one function is a little slower, however I am not sure right now which one ;) but it's really something that is no deal-breaker when optimizing for code I guess.

According to this topic echo is a little faster: Should I use echo or print in php scripts?

Community
  • 1
  • 1
Derfder
  • 3,204
  • 11
  • 50
  • 85
  • 1
    More info on print & echo def (although it has nothing to do with the original question) : http://stackoverflow.com/questions/7094118/reference-comparing-phps-print-and-echo – Laurent S. Jun 12 '13 at 15:49
  • in the same vein i think echo is also perhaps marginally faster to type.. print if you use type properly requires use of the pinky finger echo on the other hand does not, apart from of course having one letter less :) – Manquer Jun 12 '13 at 16:00
  • 1
    just FYI, that is not possible for other languages keyboards, e.g., for Latin America layout, the '/' is over the 7 key (`shift+7` = /), and the # is over the 3 key, so the difference is only between 1 vs 2 keystrokes, because neither option is _easier_ to type. That is the reason I prefer, and use a lot, the editor shortcuts to comment code. I use Notepad++, and `Ctrl+Q` [un]comments any code line. – DiegoDD Jul 24 '13 at 16:08
  • @DiegoDD Good point! I use also Sublime's shortcuts for commenting block or selections etc. – Derfder Jul 24 '13 at 16:31