NewbieBryan Posted July 11, 2009 Share Posted July 11, 2009 I have two sets of code This one calculates if a number is odd or even <?php // let's say you have a number $number = 24; // to test whether $number is odd you could use the arithmetic // operator '%' (modulus) like this if( $odd = $number%2 ) { // $odd == 1; the remainder of 25/2 echo 'ODD Number!'; } else { // $odd == 0; nothing remains if e.g. $number is 48 instead, // as in 48 / 2 echo 'EVEN Number!'; } ?> This one adds 1 to a number until 5 <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; }; ?> I tried combining them but keep on getting errors; where am I messing up? Erroneous code below <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; if( $odd = $i%2 ) { echo 'ODD Number!'; } else { echo 'EVEN Number!'; $i++; }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/165562-solved-loop-in-a-loop-problems/ Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 Would help if you posted what the error is. But anyways, it looks like you're missing a closing bracket for your else. Quote Link to comment https://forums.phpfreaks.com/topic/165562-solved-loop-in-a-loop-problems/#findComment-873269 Share on other sites More sharing options...
NewbieBryan Posted July 11, 2009 Author Share Posted July 11, 2009 Apologies Crayon Violent, it is 03h00 here local time and my brain is fried. The error is exactly that, I can't seem to be able to figure where to insert the last } ... I have though on a couple of occasions managed to get the thing into an endless loop where is displays 1 ODD Number! . . . . and repeat until I terminate the window. Quote Link to comment https://forums.phpfreaks.com/topic/165562-solved-loop-in-a-loop-problems/#findComment-873274 Share on other sites More sharing options...
cunoodle2 Posted July 11, 2009 Share Posted July 11, 2009 Here you go... <?php $i=1; while($i<=5) { echo "The number ".$i ." is an "; if($odd = $i%2 ) echo "ODD Number!<br />\n"; else echo "EVEN Number!<br />\n"; $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165562-solved-loop-in-a-loop-problems/#findComment-873278 Share on other sites More sharing options...
NewbieBryan Posted July 11, 2009 Author Share Posted July 11, 2009 Tx cunoodle2... MUCH cleaner than the way I was trying to do it. Quote Link to comment https://forums.phpfreaks.com/topic/165562-solved-loop-in-a-loop-problems/#findComment-873280 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.