Jump to content

Recommended Posts

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>

<?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.

 

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;
}
}
  }
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.