Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop vs while loop vs foreach loop PHP

Tags:

1st off I'm new to PHP. I have been using for loop,while loop,foreach loop in scripts. I wonder

  • which one is better for performance?
  • what's the criteria to select a loop?
  • which should be used when we loop inside another loop?

the code which I'm stuck with wondering which loop to be used.

for($i=0;$i<count($all);$i++) {  //do some tasks here  for($j=0;$j<count($rows);$j++)  {   //do some other tasks here   } } 

It's pretty obvious that I can write the above code using while. Hope someone will help me out to figure out which loop should be better to be used.

like image 498
Techie Avatar asked Oct 11 '12 19:10

Techie


People also ask

Which is faster foreach or for loop in PHP?

The foreach loop is considered to be much better in performance to that of the generic for loop. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively.

What is the advantage of foreach loop in PHP?

The 'foreach' loop in PHP helps in accessing key value pairs within an array. The 'foreach' loop works with arrays only, with the advantage that a loop counter wouldn't need to be initialized. In addition to this, no condition needs to be set that would be needed to exit out of the loop.


2 Answers

which one is better for performance?

It doesn't matter.

what's the criteria to select a loop?

If you just need to walk through all the elements of an object or array, use foreach. Cases where you need for include

  • When you explicitly need to do things with the numeric index, for example:
  • when you need to use previous or next elements from within an iteration
  • when you need to change the counter during an iteration

foreach is much more convenient because it doesn't require you to set up the counting, and can work its way through any kind of member - be it object properties or associative array elements (which a for won't catch). It's usually best for readability.

which should be used when we loop inside another loop?

Both are fine; in your demo case, foreach is the simplest way to go.

like image 52
Pekka Avatar answered Oct 06 '22 18:10

Pekka


which one is better for performance?

Who cares? It won't be significant. Ever. If these sorts of tiny optimizations mattered, you wouldn't be using PHP.

what's the criteria to select a loop?

Pick the one that's easiest to read and least likely to cause mistakes in the future. When you're looping through integers, for loops are great. When you're looping through a collection like an array, foreach is great, when you just need to loop until you're "done", while is great.

This may depend on stylistic rules too (for example, in Python you almost always want to use a foreach loop because that's "the way it's done in Python"). I'm not sure what the standard is in PHP though.

which should be used when we loop inside another loop?

Whichever loop type makes the most sense (see the answer above).

In your code, the for loop seems pretty natural to me, since you have a defined start and stop index.

like image 45
Brendan Long Avatar answered Oct 06 '22 17:10

Brendan Long