senglor Posted June 21, 2011 Share Posted June 21, 2011 for() loop to print all of the EVEN numbers between 0 and 30, inclusive. <?php for ($i=0;$i<=30; ????? echo $i ?> Quote Link to comment https://forums.phpfreaks.com/topic/240032-for-loopsnewbie-to-php/ Share on other sites More sharing options...
Alex Posted June 21, 2011 Share Posted June 21, 2011 From the starting value ($i = 0) what do you need to do to get to the next even number? Also, in the future please use [[/tt]php] or [[tt]code] tags. Quote Link to comment https://forums.phpfreaks.com/topic/240032-for-loopsnewbie-to-php/#findComment-1232995 Share on other sites More sharing options...
Wilio1 Posted June 21, 2011 Share Posted June 21, 2011 Well you need to check if the number that is divided by 2 returns a float or an integer. Quote Link to comment https://forums.phpfreaks.com/topic/240032-for-loopsnewbie-to-php/#findComment-1232999 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 No you don't. If $i starts at 0, what do you need to do to $i to get to the next even number? Quote Link to comment https://forums.phpfreaks.com/topic/240032-for-loopsnewbie-to-php/#findComment-1233006 Share on other sites More sharing options...
Wilio1 Posted June 21, 2011 Share Posted June 21, 2011 Ah yes, I didn't see the easier method. Quote Link to comment https://forums.phpfreaks.com/topic/240032-for-loopsnewbie-to-php/#findComment-1233008 Share on other sites More sharing options...
Bl4ckMaj1k Posted June 21, 2011 Share Posted June 21, 2011 Just increment by 2 until it reaches thirty...was this a test? did i pass? lol Quote Link to comment https://forums.phpfreaks.com/topic/240032-for-loopsnewbie-to-php/#findComment-1233013 Share on other sites More sharing options...
senglor Posted June 22, 2011 Author Share Posted June 22, 2011 Well you need to check if the number that is divided by 2 returns a float or an integer. I need to increment i by 2, so looking at this i've figured it out. <?php //for_loop.php for ($x=0; $x<=30; $x=($x+2)) { echo "$x"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240032-for-loopsnewbie-to-php/#findComment-1233430 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.