PHP Syntax: Three Ways to Create Comments

   Posted December 13, 2012 by David Kirk in PHP programming

PHP allows three different methods of formatting comments. All of these sytax examples will seem familiar to programmers of other languages.

Few things in programming stimulate as much conflict as commenting. Many code gods can just read the code like most of us read the newspaper. For these coding savants, commenting is seen as a frivolous waste of time.

However, for code monkeys like myself who can barely keep track of what was going on 5 minutes ago, it makes a lot of sense to me that code be heavily commented. Trying to struggle through poorly commented code is a torture.

Whatever your feelings on commenting, you will need to know the three different methods used to create comments within PHP code.

Comment Block

Slash-asterisk pairs are used to surround a multi-line comment block.

/* This is an example of slash-asterisk pairs being used to create a comment block. This comment block can be as long as required. This method is frequently used to "comment out" large blocks of code in testing. */

example of block comments in php

Inline Comments

The double slash (//) and pound sign (#) are the two methods used to create inline comments. Everything following either of these will be ignored and will be considered a comment.

$var++; // anything to the right of the double slash will be ignored
$var=2; # right of the pound sign is considered a comment

examples of inline commenting in PHP

 

About David Kirk

David Kirk is one of the original founders of tech-recipes and is currently serving as editor-in-chief. Not only has he been crafting tutorials for over ten years, but in his other life he also enjoys taking care of critically ill patients as an ICU physician.
View more articles by David Kirk

The Conversation

Follow the reactions below and share your own thoughts.

  • wladvisor

    It would be great to also add the ” character to put in comment everything coming just after like in ABAP/4 programming language. Maybe an old school language but used in many top 100 companies.

    So, we would have something like :
    $var++; ” anything to the right of the ” will be ignored

  • Micahthewise1

    the pound sign (#) has been depreciated as of php 5.3.x (source: http://php.net/manual/en/migration53.deprecated.php)