PHP - Chapter 3 - Using Comments

Introduction

It is always good practice to comment your program or script. This is so anyone who uses your code can easily figure out what a function is doing or why you chose a particular method for doing something. It's also so you will know what your thoughts were if you ever need to revisit a particular script.

There are three ways to make a comment in PHP. The in-line (also called single-line) comment is led by double slashes (//). A multi-line comment is stored in between a C-style comment marker (/* ... */). These two are the most popular ways to specify a comment. A lesser known and lesser used comment marker is the shell style comment marker (#). This is also a single line comment marker.

Type or copy the code below into a text editor, save the file as "script03.php", and upload it to your server.

Definitions (in layman's terms)

Comment: Textual information that is not interpreted by the computer, but is in place for human readability

Code:
<html>
<head>
	<title>Comments in PHP</title>
</head>
<body>
	<?php
		echo("<p>Testing in-line comments:</p>");
			// This is an in-line comment

		echo("<p>Testing multi-line comments:</p>");
			/* This is a multi-line, C-Style comment.
			   it can span over more than one line.*/

		echo("<p>Testing shell-style comments:</p>");
			# This is a shell-style comment line
	?>
</body>
</html>
Breakdown

As you can see, the comments aren't interpreted by the computer. They aren't displayed in the browser, nor are they present in the XHTML source.

It is always a good idea to document your code, comments are a good way to do this. Another use for comments is temporarily disabling lines of code for debugging purposes.

< Back - Working with Text PHP Index Next - Variables >