الثلاثاء، 24 ديسمبر 2013

is there any shorthand method to handle loops if you know exactly how much times a loop needs to be executed

When the number of loops is known, the best option is the for loop. As your example shows, it will loop until the desired number of times, in your case 5 (the original loop and goes up 5 times for a total of 6, unless you have 7 entries in which case the 6 is your required value).

Foreach comes in handy particularly for arrays.

A while loop is used when calling information from a database.

@DigitalChris recommends str_repeat, but I think the for loop is clearer in terms of code. The best solution isn't always to be quick to do right away, it's often to be able to make quick changes if the need comes. Personal opinion, anyways.


View the original article here

ليست هناك تعليقات:

إرسال تعليق