• 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)
  • 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.