Chapter 0:Introduction 0-000.000 - About This Course
Chapter 1:PHP Basics 1-1.1 - Hit the ground running.
1-1.2 - A Simple PHP Program
1-1.3 - Formatting the code
1-1.4 - Comments in PHP
1-1.5 - Storing Data in Variables
1-1.6 - The print() Function
|
Chapter: 1 - PHP Basics
Lesson: 1.4 - Comments in PHP
One of the key points of programming is making comments judiciously. You should place comments in your code where you need to identify what something does, the data type or other useful information. Comments can be made for your own benefit when you go back to read the code again later or they can be for the benefit of a project group or for instances where you may share or distribute your source files.
A good comment identifies something, gives direction, or provides information about a function or process. There is no need to comment excessively, that just makes for bigger file sizes and more overhead. Keep in mind that comments present within the PHP tags ( and ) will not display in the users browser, in fact, they don't even show up in the source code when the browser receives it. Comments only show up in the original PHP source code.
You can still use the standard comment methods in your HTML segments, but these are passed on to the browser. When you want to keep your comments hidden, do it in the PHP segments of your code. Figure 1.4 shows the different methods of commenting within your PHP code and shows when you would use HTML comments vs. PHP comments.
Figure 1-1.4, Comments (download code here)
(Try the sample code)
You'll note in Figure 1.4 that the comments in the first segment use two forward-slashes to precede each line of the comment. This is fine for one line comments. In the second sample, you see the standard HTML comment form with the less than bracket, an exclamation point and two hyphens followed by the comment and closing tag of two hyphens and a greater than bracket. Finally, in the third example of commenting, the forward-slash and asterisk is followed by your comment (over as many lines as you wish) and the comment is followed by another asterisk and another forward-slash (reverse order for closing the comment).
Remember, use comments frequently when needed but, not to an excess. Commenting helps the readability of your code in some cases, but over-commenting can help stifle the legibility of your code as well.
|