redarrow Posted March 18, 2006 Share Posted March 18, 2006 Can someone give more examples please fully exsplained as i am trying to learn the while loop.As meny examples as possable while loop only thank you1.set conditon i=02.while() {}3.i<64.echo statement5.i++ meaning 1 more[code]<?$i=0;while($i<6){echo $i;$i++;}?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 18, 2006 Author Share Posted March 18, 2006 I found this on the net to learn all the loops properly i gooing to use it as a refrence and learn it all till i goto the next problam what is arrays dam good luck all I need it lolIf a user could brake each line down and exsplain all then grate thank you so much.[code]<html> <head> <title>Loops</title> </head> <body> <h2>FOR loop</h2> <p>We will print "Hello, World!!!" 10 times</p> <?php for ($i = 0; $i < 10; $i = $i + 1) { echo "Hello, World!!!<br>"; } ?> <h2>WHILE loop</h2> <p>We will print "Hello, World!!!" for 0.01 seconds</p> <?php $counter = 0; $start_time = microtime(); while ($start_time > (microtime() - 0.01)) { echo "Hello, World!!! "; $counter = $counter + 1; } echo "<br>Loop executed " . $counter . " times."; ?> <h2>DO-WHILE loop</h2> <p>We will print "Hello, World!!!" for 0.01 seconds</p> <?php $counter = 0; $start_time = microtime(); do { echo "Hello, World!!! "; $counter = $counter + 1; } while ($start_time > (microtime() - 0.01)); echo "<br>Loop executed " . $counter . " times."; ?> <h2>FOREACH loop</h2> <p>We will print values from array</p> <?php $address_book = array ( "Jessica" => "(415) 555-2946", "Michelle" => "(925) 555-1274", "Linda" => "(707) 555-3349" ); foreach($address_book as $name=>$phone) echo $name . " - " . $phone . "<br>"; ?> <h2>BREAK and CONTINUE</h2> <p>We will print as many even number, which can be divided by 4, in 0.01 second</p> <?php $start_time = microtime(); for ($i = 1; $i <= 1000000; $i = $i + 1) { if ($i % 2 == 1) continue; // Number is odd, there is no point to continue // loop to check if it can be divided by 4 // so we are going to next loop iteration if ($i % 4 == 0) echo "Number " . $i . " is dividable by 4<br>"; if ($start_time < (microtime() - 0.01)) break; // If we are out of time we will stop the loop // Notice: at this point condition is still true // but the loop quits } ?> </body></html>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 18, 2006 Author Share Posted March 18, 2006 please help dont get it at all num=10 while num less then ten echo hi else if num less then 9 echo hello.I am trying to use the while loop and if in the same code how cheers.[code]<?$num=10;while($num <10){echo "hi";}else{if($num < 9){echo"hello";}}$num++;?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 18, 2006 Author Share Posted March 18, 2006 how can u count from 1 to 10 with the while loop and for loop and while do loop.example using all the statments in one.for echo 1while echo 2while do echo 3for echo 4ect ect ect i tried and faild 3 hours please help thank you.the idear is to echo the 1 to 10 out useing one varable $num = 10;and use all the loops and display 1-10THANK YOU. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 19, 2006 Share Posted March 19, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]how can u count from 1 to 10 with the while loop and for loop and while do loop.[/quote][code]<?php//// for loop//for($i=1;$i<11;$i++) echo $i.'<br />';//// while loop//$i = 1;while ($i<11) { echo $i.'<br />'; $i++; }//// ?>[/code]I've never used the "while do" loop structure, since it is just slightly different from a while loop.Ken Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 19, 2006 Author Share Posted March 19, 2006 [!--quoteo(post=356295:date=Mar 19 2006, 12:32 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 19 2006, 12:32 AM) [snapback]356295[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]<?php//// for loop//for($i=1;$i<11;$i++) echo $i.'<br />';//// while loop//$i = 1;while ($i<11) { echo $i.'<br />'; $i++; }//// ?>[/code]I've never used the "while do" loop structure, since it is just slightly different from a while loop.Ken[/quote]thanks ken getting there cheers.[!--sizeo:5--][span style=\"font-size:18pt;line-height:100%\"][!--/sizeo--]solved[!--sizec--][/span][!--/sizec--] Quote Link to comment Share on other sites More sharing options...
Laamgat Posted April 17, 2008 Share Posted April 17, 2008 I'm working on a script that will display a random image each week. The script calls for a random number, but I don't ever want the number to be the same after being executed twice. So, I've tried while loops and do-while loops and every time, the script just freezes and continues to loop continuously.[code]<?php// Get current week number. Assign to a variable.$current_week = date("W");// Open up and read current_week.txt. Assign to a variable.$previous_week_array = file("current_week.txt");$previous_week = $previous_week_array[0.1];// Open up and read current_shirt.txt. Assign to a variable.$current_shirt_array = file("current_shirt.txt");$current_shirt = $current_shirt_array[0];if ($current_week > $previous_week) { $random_number = mt_rand(1,7); while ($randon_number = $current_shirt) $random_number = mt_rand(1,7);}echo $random_number;?>[/code]Can anyone tell me why this is happening? Quote Link to comment 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.