Comment Your PHP Code

August 6th, 2012 by Laeeq | 4 comments

In my previous post, you had learned how to How to write good PHP Code . In this post I have explained how to comment your PHP Code

Why You Should Comment Your PHP Code:

Code commenting  is a code that is not executed or read as part of the program. The code comments are used to make code more readable and easy to understand. The code with proper comments considered as good programming that let’s other programmers to understand what you are doing in each step. Actually when you are working with a group of peaple then it is necessary to use proper code comments to share code with them.

Good Code commenting also helps you to remind what you did previously. comments can remind you about functons, methods and helps to editing the code.

How to comment your PHP Code:

You can commnet your php code by using double forward slash “//”

  1. <?php
  2. echo “This is my php code”;
  3. //echo ”php code”;
  4. ?>

You can use hash “#” to comments your php code when it is ingle line

  1. <?php
  2. echo “php code comments ”;
  3. #echo ”php code standard”;
  4. ?>

You can also comments your php code by using   “/* */” . This useful becuase, by using this you can comments several lines of code block.

  1. <?php
  2. echo “my php code comments example”;
  3. /*echo ”my php code comments example1″;
  4. echo ”my php code comments example2″;
  5. echo ”my php code comments example3″;
  6. echo ”my php code comments example4″;*/
  7. ?>

You can also describe functions or method inside this “/* */”

  1. <?php
  2. /*
  3. This function is written for printing message.
  4. you just pass message argument.
  5. */
  6. function printMessage($message)
  7. {
  8. echo $message;
  9. }
  10. ?>

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • 10 Smart Tips to Become a Better PHP Developer
  •  

     

    1. August 6th, 2012 at 20:51 | #1

      I think if you write an article about commenting code, you should mention a bit more like phpdoc & co. Just adding a comment above a method isn’t good practice, there’s more to it.

    2. August 8th, 2012 at 15:25 | #2

      I use code commenting to give my self a hint of which code I am writing for and which part of the code functions run in that area.

    3. August 10th, 2012 at 10:05 | #3

      its really helpful!

    4. Alvaro
      August 10th, 2012 at 19:42 | #4

      fantastic stuff!

    1. No trackbacks yet.