I, j and k should not be used

Deprecate i, j and k

I, j and k are generally variable names that are used within for loops. I believe that things have changed in regards to programming and that we should not be using i, j and k anymore. But before talking about that let's discuss how i, j and k are used.

Using the same letters for each loop has made our code more readable and recognisable. I is the most popular character that is used followed by j and then k.

for ($i = 0; $i < $arrayTotal; $i++) { for ($j = 0; $j < $arrayTotal; $j++) { for ($k = 0; $k < $arrayTotal; $k++) { /* some code */ } } }

Over time programming languages have advanced and most now provide a foreach construct or something very similar. This has enabled us to replace our for loops with a simpler statement. There is also a shift to using iterators to loop through objects which means for loops are used less and less:

/* old way of looping */ for ($i = 0; $i < $arrayTotal; $i++) { echo $array[$i] . ''; } /* new way of looping */ foreach ($array as $value) { echo $value . ''; }

This now poses the question, do we need to use i, j and k? If i, j and k were used as array counters and have been superseded by a simpler way of doing things, then we should deprecate the use of these characters as variable names. They were originally ok to use as it was common practice and common practice creates consistency. It is now common practice to use a foreach loop which means i, j and k should not be used and should be replaced with something more meaningful.

If you need to loop through an array, a collection, a class, a dictionary or whatever else, use a foreach construct. It will make your code more readable. If for some reason you do need to use a for loop, then use a meaningful name for the counter such as $totalPeople or $totalTax:

/* always use meaningful counters */ $totalPrice = 0; for ($currentProduct = 0; $currentProduct < $totalProducts; $currentProduct++) { $totalPrice += $products[$currentProduct]; } /* give all variables meaningful names to make your code more readable */ $totalPrice = 0; foreach ($products as $productPrice) { $totalPrice += $productPrice; }