Name a variable, delete a comment

Confusing street signCommenting code can increase understanding for people that need to look at and edit your code at a later date. It is not only good programming practice but is common courtesy. By not leaving your code in a readable state you are making it very difficult for that code to be updated by anyone but yourself. One of the biggest problems with commenting is that it is done poorly. Programmers comment everything they are doing as though the next programmer doesn't understand how to program.

The real goal of programming is not to comment.

//looping through total products for ($i = 0; $i < $total; $i++) {

The above code has a superflous comment, what we really need to do is eliminate the comment and change our code.

for ($i = 0; $i < $totalProducts; $i++) {

Another example:

$counter = 0; //total products that are a rip off foreach ($products as $row) { if ($row['cost'] > 5) { $counter++; } }

Having better names for variables can also elminate comments:

$totalExpensiveProducts = 0; foreach ($products as $row) { if ($row['cost'] > 5) { $totalExpensiveProducts++; } }

If you find that you are writing too many comments, try refracturing your code to better describe what you are trying to achieve.