redarrow Posted May 11, 2007 Share Posted May 11, 2007 hi there advance thank you i am trying to understand a loop within a loop can any one exsplain the behavour of the following example cheers. ps. i am not wasting your time i am currently brushing up on php. if you would like to you can post your example for me to understand the concept cheers. <?php for($a=0; $a<4; $a++){ for($b=0; $b<3; $b++){ for($c=0; $c<2; $c++){ echo" a: loop $a <br> b: loop $b <br> c: loop $c <br>"; } } } ?> result a: loop <br> b: loop 0 <br> c: loop 0 <br> a: loop <br> b: loop 0 <br> c: loop 1 <br> a: loop <br> b: loop 1 <br> c: loop 0 <br> a: loop <br> b: loop 1 <br> c: loop 1 <br> a: loop <br> b: loop 2 <br> c: loop 0 <br> a: loop <br> b: loop 2 <br> c: loop 1 <br> a: loop <br> b: loop 0 <br> c: loop 0 <br> a: loop <br> b: loop 0 <br> c: loop 1 <br> a: loop <br> b: loop 1 <br> c: loop 0 <br> a: loop <br> b: loop 1 <br> c: loop 1 <br> a: loop <br> b: loop 2 <br> c: loop 0 <br> a: loop <br> b: loop 2 <br> c: loop 1 <br> a: loop <br> b: loop 0 <br> c: loop 0 <br> a: loop <br> b: loop 0 <br> c: loop 1 <br> a: loop <br> b: loop 1 <br> c: loop 0 <br> a: loop <br> b: loop 1 <br> c: loop 1 <br> a: loop <br> b: loop 2 <br> c: loop 0 <br> a: loop <br> b: loop 2 <br> c: loop 1 <br> a: loop <br> b: loop 0 <br> c: loop 0 <br> a: loop <br> b: loop 0 <br> c: loop 1 <br> a: loop <br> b: loop 1 <br> c: loop 0 <br> a: loop <br> b: loop 1 <br> c: loop 1 <br> a: loop <br> b: loop 2 <br> c: loop 0 <br> a: loop <br> b: loop 2 <br> c: loop 1 <br> Quote Link to comment https://forums.phpfreaks.com/topic/50943-solved-for-loop-within-a-for-loop-please-help-cheers/ Share on other sites More sharing options...
per1os Posted May 11, 2007 Share Posted May 11, 2007 <?php //while a is less than 4 do this and increment a for($a=0; $a<4; $a++){ // while b is less than 3, increment b and do this for($b=0; $b<3; $b++){ // while c is less than 2, increment c and do this. for($c=0; $c<2; $c++){ // print out a: loop num b: loop num c: loop num echo" a: loop $a <br> b: loop $b <br> c: loop $c <br>"; } } } ?> Ok so anytime c loop is hit that message is displayed. Since the C for loop is inside the b for loop anytime b is ran through that message gets displayed twice. Since the b loop is inside the a loop anytime the a loop is ran the b loop runs 3 times which in turn runs the c loop 3 times 2 (6 times). So all in all the loop should run (4 * 3 * 2) which is 24 times. You should get 24 messages displays to your screene. Essentially. Hope that helps and hope I didn't mis-interpret what is going on. Quote Link to comment https://forums.phpfreaks.com/topic/50943-solved-for-loop-within-a-for-loop-please-help-cheers/#findComment-250587 Share on other sites More sharing options...
redarrow Posted May 11, 2007 Author Share Posted May 11, 2007 Thanks frost for your help and example, that what i needed cheers mate thank you for your time. Quote Link to comment https://forums.phpfreaks.com/topic/50943-solved-for-loop-within-a-for-loop-please-help-cheers/#findComment-250593 Share on other sites More sharing options...
redarrow Posted May 11, 2007 Author Share Posted May 11, 2007 Does this look correct to you. <?php for($a=0; $a<10; $a++){ for($i=0; $i<10; $i++){ switch($i.$a){ case(; $message1=<<<word1 I am redarrow word1; echo " $message1 <br> "; break; case(9); $message2=<<<word2 I love php word2; echo $message2; break; case(10); $message3=<<<word3 Php rocks! word3; echo "$message3 <br> "; default; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50943-solved-for-loop-within-a-for-loop-please-help-cheers/#findComment-250651 Share on other sites More sharing options...
taith Posted May 11, 2007 Share Posted May 11, 2007 switch($i+$a){ Quote Link to comment https://forums.phpfreaks.com/topic/50943-solved-for-loop-within-a-for-loop-please-help-cheers/#findComment-250655 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.