Use the // comment type for single-line or short comments, and the /* .... */ syntax for large block comments.
// this is a single line comment
// this is a short comment that tells
// you something about the following code
/*
this is a long comment
and it
goes on
for several
lines...
*/
Comments should precede the code they describe, rather than following it.
$var = 0; // initialize $var
if ($var) // check $var
// initialize $var
$var = 0;
// check $var
if ($var)
When commenting loops and branches, it is acceptable to put the comment inside the braces.
if ($var)
{
// $var is true so do the following code
echo $var;
}
Separate 'do' branches and major code blocks with the comment string shown here. I suggest you copy this line and add it as an insertable code snippet in your PHP editor. If you want to insert a short definition of what the 'do' code will do, add it immediately following the separating comment line.
require_once('./global.php');
// #############################################################################
if (empty($_REQUEST['do']))
{
$_REQUEST['do'] = 'modify';
}
// #############################################################################
if ($_REQUEST['do'] == 'edit')
{
// do edit code
}
// #############################################################################
// lists items in a table
if ($_REQUEST['do'] == 'modify')
{
// do modify code
}
Prefix PHP function definitions with the comment string shown here. Once again, you should create a code snippet with this string.
// ###################### Start is_something ######################
function is_something($var)
{
return iif($var, true, false);
}
// ###################### Start fetch_uppercase ######################
function fetch_uppercase($var)
{
return strtoupper($var);
}