Skip to main content

PHP: Adding Comments To Your Scripts

Any good programmer will tell you that adding comments to your code can be extremely useful. Since you are on your way to becoming a PHP programmer, it is wise to heed the advice of other programmers.

Comments in your code do nothing to the script itself. Their main purpose is to help you understand why you did something. PHP supports three methods of adding comments to your code: #, // and /* */.

Notes:

  • You can not use HTML style comments (<!-- HTML comment -->)in your PHP code (between <?php ?>)
  • PHP comments are never shown in HTML (i.e the page source)

Below are all valid methods of commenting your PHP scripts.

Single line commenting:

# This comment uses the hash sign to begin a comment
// This comment uses two slash marks to begin a comment

Multi-line commenting:

/* This chunk of text
    will be ignored by PHP. */

You can also use comments at the end of statement:

echo "Hello PHP user";  // This is a vaild comment as well

Comments are also good for adding a description of what the script does, the creator, version numbers, etc...

/* 
 Filename: user.php
 Description: This script allows visitors to register on this site.
 Creator: Brandon
 Version: 1.0
 Copyright: GPL 2
*/

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <p><b><i><pre><a><img><em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><h1><h2><h3><h4><h5><h6>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

Premium Drupal Themes by Adaptivethemes