Title: the loop: foreach or while?
Last modified: August 20, 2016

---

# the loop: foreach or while?

 *  [mtemp](https://wordpress.org/support/users/mtemp/)
 * (@mtemp)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/the-loop-foreach-or-while/)
 * Hi,
 * I want to implement WP indivudally on several pages of my site.
 * Is there a difference of using the loop with “foreach”:
 *     ```
       foreach ($posts as $post):
       ...
       the_post();
       endforeach;
       ```
   
 * … or with “while”?
 *     ```
       while ( have_posts() ) : the_post();
       ...
       endwhile;
       ```
   

Viewing 1 replies (of 1 total)

 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/the-loop-foreach-or-while/#post-2902638)
 * Not much difference, really. There is some significant debate, by volume, over
   which is faster. `foreach` seems to me to win that but the difference is extremely
   small. With some tests the difference is something like 2/10th of a second looping
   through 1.5 million element arrays. With some other tests I’ve seen the differences
   don’t show up until the 4th or 5th decimal place. I wouldn’t worry too much about
   it.
 * I habitually use `foreach` in PHP but not really for performance. I like the 
   option of being able to deal with the keys and values, which isn’t possible with`
   while`.

Viewing 1 replies (of 1 total)

The topic ‘the loop: foreach or while?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/the-loop-foreach-or-while/#post-2902638)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
