Lesson 7: Comment your scripts

As you may have noticed, PHP scripts can easily look confusing. In this lesson, we 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:



	// 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!

Related topics in the PHP ForumRepliesViews
PHP-Lesson 7: comment on scripts, at double slashes27498
mysql query for comment99725
Comment your Script (Lesson 7)38832

+ Post a new topic

<< Lesson 6: Conditions

Lesson 8: Arrays >>