Posted by : senan Wednesday, February 12, 2014

Welcome! Comments are used to describe your code. When you write a block of code to accomplish something, it is good practice to write what you have done in that code. More precisely explain what the code does using comments.
Now let's see how we write comments in our code.

Types of Comments in PHP


There are two ways to write comments in your PHP code.
  • Single Line Comments
  • Multple Line Comments
Single Line comments in PHP
Single line comments are written using two forward slashes, "//". Any line in your PHP code with // in front will be ignored and treated as comments. Let’s take a look at an example.

Example - Single Line Comments

<html>
 <title>my php page</title>
 <body>
 <?php
  // print hello there on the screen
  echo "hello there!";
  //echo "this php line will be ignored by the interpreter";
 ?>
 </body>
</html>
In the above example we described the echo line code using a single line comment "// print hello there on the screen".
Also notice that the we put // in front of the echo on the last line. Even thou that line is a valid PHP code but it is treated as a comment and is ignored by the PHP interpreter.
Multiple Line Comments in PHP
Multiple line PHP comment begins with "/*" and ends with "*/". So, anything inside /* */ will be treated as comments by the PHP interpreter.
Multiple line comments are normally used when you have to explain a rather long block of code instead of one line code as we saw above with the echo statement. For example, commenting what a function does would be done using /* */. Let's have a look at an example to see what i mean.

Example - Multiple Line Comments

<html>
 <title>my php page</title>
 <body>
 <?php
  
  /*
   * print_name: print message on the screen
   * @param: $name - name to be used in the message
   */
  
  function print_name($name)
  {
   // print hello there on the screen
   echo "hello $name!";
  }
 ?>
 </body>
</html>

Why write comments in your code?

Comments are an integral part of your code. Most of the times you will be writing lots of code and things will get complicated. Without comments in your code, you could get lost as to what your code does when you come back to it later. Comments will help you remember what you did and what your code does.
It also helps to write comments when you are working in a team. Other programmers can easily tell what your code does by reading your comments. They don't have to bother to read your code line by line to figure out what it does. This can be really painful for the other developers.

Tips on writing comments

When writing comments describe what the code does instead describing how the code works. Newbie’s and students usually make this mistake. Use single line comments when describing a single line of code and use multiple line comments when describing a block of code.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Learning Programming Language - Skyblue - Powered by Blogger - Designed by Johanes Djogan -