Foreach vs While

Joined
Jul 4, 2016
Messages
25
Points
1
Hey Guys,
I want to use Foreach or While to show results on a page them of Wordpress, but i don't know which command I should use and what is the advantage between them? When I should use Foreach or While
I am wanting to display a list of records from database and I often use while function in PHP sites but for wordpress, I am not sure which one should be used is the best.
 

RDO Servers

Well-known member
Registered
Joined
Apr 3, 2015
Messages
1,027
Points
83
'Foreach' is typically used to do a task, for each record in a database, data table, array, etc.

'While' is used to run a section of code while a variable is 'true'.
 

energizedit

Well-known member
Registered
Joined
Dec 13, 2016
Messages
259
Points
18
RDO has given the best description of the two. Here are some examples. Most likely you will want to use foreach. That would be used when you have a set of data like an array, an array returned from a database, like in wordpress.

While, would be used to test a variable until something is true. Like, while
Code:
while($x <= 5) {
    echo "The number is: $x <br>";
    $x++;
}
 
Joined
Jul 4, 2016
Messages
25
Points
1
'Foreach' is typically used to do a task, for each record in a database, data table, array, etc.

'While' is used to run a section of code while a variable is 'true'.
Exactly I used while to run a section of code to display records from database as my example below.

RDO has given the best description of the two. Here are some examples. Most likely you will want to use foreach. That would be used when you have a set of data like an array, an array returned from a database, like in wordpress.

While, would be used to test a variable until something is true. Like, while
Code:
while($x <= 5) {
    echo "The number is: $x <br>";
    $x++;
}
Thanks for your advices and yes I used to see Wordpress used foreach on their codes but when I make custom codes i often go with while, this seems more better and helping more.
for example, this is a custom code that used to create a list of videos categories with custom design on my WP site.

Code:
<?php 
while (have_posts()) :
the_post(); 					
?>	
<div class="showcatevid">
	<div class="indevideos">
			<a href="<?php the_permalink(); ?>">
				<?php if (has_post_thumbnail( $post->ID ) ): ?>
				<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnails' ); ?><img src="<?php echo $image[0]; ?>">
				<?php else : ?>
				<img src="<? bloginfo('template_directory');?>/images/defaultthumbnail.jpg">
				<?php endif; ?>
			</a>
	<h3><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a></h3>
    by <span class="spblu"><?php the_author();?></span><br />
    <?php echo $post->post_view;?> views<br />
	</div>
</div>
<?php			
endwhile;
wp_reset_query();
?>
 
Older Threads
Replies
9
Views
8,950
Replies
5
Views
2,891
Replies
7
Views
3,616
Replies
26
Views
22,854
Replies
8
Views
8,987
Newer Threads
Replies
4
Views
4,265
Replies
5
Views
4,932
Replies
17
Views
7,524
Latest Threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top