while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djcrisp
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 6:25 pm

while loop

Post by djcrisp »

anyone how to use the while loop that increments through and prints every odd number between 1 and 49.

thanks in advance :-)

any help is appreciated.
my guess is:


Code: Select all

<?php
$i = 1;
while ($i <= 49)
 { 
    echo $i++;
  }
?>
User avatar
Peter Anselmo
Forum Commoner
Posts: 58
Joined: Wed Feb 27, 2008 7:22 pm

Re: while loop

Post by Peter Anselmo »

It's better practice to use a "for" loop if you know ahead of time exactly how many iterations you'll need (as is true in this case).

Code: Select all

 
for( $i=1; $i<=49; $i+=2) {
 echo $i;
}
Post Reply