Monday, 29 June 2015

Comment Your Scripts - PHP Tutorial 7

Comment your scripts

As you may have noticed, PHP scripts can easily look confusing. In this lesson, we will cover why comments are important and how to insert them into your scripts.

Why is it important to comment your scripts?

When you are coding, you are writing commands to a server/computer and need to use a highly formal language that may not clearly reflect your thoughts when making the script.
Therefore, it can be difficult for others (or yourself) to understand how the script is structured, and thus hard to identify and correct errors in the script.
Comments can be used to write short explanatory text in the script. The server completely ignores the comments, and the comments do not affect the actual functionality of the script.
In the business world, it is often a requirement that scripts and programming are commented, otherwise it would be too risky for a company to take over a system in which it would be too difficult to find and correct errors.

How do you insert comments?

It is quite easy to insert a comment. You simply start the comment with a double slash: "//".
Take a look at this example from lesson 5, now with comments:

 <html>
 <head>
 <title>Loops</title>
 </head>
 <body>

 <?php

 // Here we write color codes using three loops

 // Red can be between 0 and 255 
 for ($intRed=0; $intRed<=255; $intRed=$intRed+30) {

    // Green can be between 0 and 255
    for ($intGreen=0; $ intGreen<=255; $intGreen=$intGreen+30) {

       // Blue can be between 0 and 255
       for ($ intBlue=0; $intBlue<=255; $intBlue=$intBlue+30) {

       // The color code is made on the form rgb(red,green,blue)
    $strColor = "rgb(" . $intRed . "," . $intGreen . "," . $intBlue . ")"

       // Now we write the color code to the client
    echo "<span style='color:" . $strColor . "'> " . $strColor . " </span>";

       // Closes the loops
       }
    }
 }

 ?>
 
 
For the sake of the example, we have included many extra comments, so it should be clear that you are far better off debugging a script with comments than without.
Therefore, remember to comment your scripts!
In the Next Lesson, you will learn what an array is, how it is used, and what it can do.

Written by

is one of the Team Member of Programmer vs Hacker. He has written many articles on this website and is a patner of this website.

0 comments:

Post a Comment

We’re eager to see your comment. However, Please Keep in mind that comments are moderated manually by our human reviewers according to our comment policy, and all the links are nofollow. Using Keywords in the name field area is forbidden. Let’s enjoy a personal and evocative conversation.

© 2015 Programmer vs Hacker. All rights resevered.