If you’ve ever had to work through another programmer’s code to complete a project, you probably understand the importance of commenting code. There’s nothing worse then trying to piece together how an application works while weeding through non-descript variables and functions. Even if for your own use, nicely commented code can be a tremendous timesaver when you leave a project and return to it down the road.
PHP is unique in that if offer several ways to comment code. There are 2 options for single-line commenting, and another option for multi-line commenting.
There are two different ways to insert single-line comments in PHP. One is based on the Unix style for shell scripts, while the other is based on comments in the C++ programming environment.
<?php
# This is a comment in PHP using the Unix style
// This is a comment in PHP using the C++ style
?>
Which style should you use? Technically speaking, there’s really no benefit or drawback to either. It’s simply a matter of preference.
What if you want to insert a large block of text that shouldn’t be interpreted as code or displayed on screen? This is the perfect opportunity to utilize a multi-line comment. Just like the 2 single-line comment styles, PHP’s multi-line comment is derived from another programming language—in this case, C.
<?php
/*
This is a multi-line comment in PHP.
The style for mult-line comments in PHP is derived from the C programming language.
Like all comments, none of this will be displayed on screen.
*/
?>





October 31st, 2007 at 1:37 pm
Using the C style of commenting should NEVER be used. I have been to quite a few php conferences and have had the chance to ask many questions directly to the people who develope php. C style comments may be depreciated in the future. If they are and you upgrade you will have alot of fixing to do.