seadonkey Posted February 16, 2008 Share Posted February 16, 2008 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>While Logic</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php $Count = 0; while ($Count > 100){ $Numbers[] = $Count; ++$Count; foreach ($Count as $CurNum) echo"<p>$CurNum</p>"; ?> </body> </html> I am unable to debug an error that is looking me in the face. Its a simple print the numbers 1-100 as long as the number is above zero. Link to comment https://forums.phpfreaks.com/topic/91414-simple-while-statement-problem/ Share on other sites More sharing options...
Chris92 Posted February 16, 2008 Share Posted February 16, 2008 what you code is doing is checking if the $Count is greater than 100, if it is then it is going to loop, you should be using a '<' not a '>', and also, for this kind of looping you would be better off using a for loop: <?php for ($i=0; $i <= 100; $i++) { $Numbers[] = $i; } echo implode("<br />" $Numbers); ?> Link to comment https://forums.phpfreaks.com/topic/91414-simple-while-statement-problem/#findComment-468397 Share on other sites More sharing options...
seadonkey Posted February 16, 2008 Author Share Posted February 16, 2008 gotcha, I knew it was something very simple however there is also a problem with my foreach <?php $Count = 0; while ($Count < 100){ $Numbers[] = $Count; ++$Count; foreach ($Count as $CurNum) echo"<p>$CurNum</p>"; } ?> I am getting a Invalid argument supplied for foreach() I am assuming I am getting that error because I attempting to us a foreach with $CurNum that is not an array? Or have I completely missed the boat. Link to comment https://forums.phpfreaks.com/topic/91414-simple-while-statement-problem/#findComment-468405 Share on other sites More sharing options...
Chris92 Posted February 16, 2008 Share Posted February 16, 2008 No basically what foreach does is loop through each different value of your array and set it to in this case $CurNum, I don;t know why you have it inside your while loop, that isnt needed. <?php $Count = 0; while ($Count < 100) { $Numbers[] = $Count; ++$Count; } foreach ($Count as $CurNum) { echo "<p>$CurNum</p>"; { ?> Link to comment https://forums.phpfreaks.com/topic/91414-simple-while-statement-problem/#findComment-468413 Share on other sites More sharing options...
Chris92 Posted February 16, 2008 Share Posted February 16, 2008 Oh, sorry but I didn't look properly your foreach loop should be looping through $Numbers not $Count. Link to comment https://forums.phpfreaks.com/topic/91414-simple-while-statement-problem/#findComment-468421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.