KingOfHeart Posted November 1, 2009 Share Posted November 1, 2009 I want to create a loop where it would basically create an output of data that would look like this .... .... .... .... 4x4 for($y; $y < $y2; $y ++) { echo "y:" . $y . "<br>"; for($x; $x < $x2; $x ++) { echo "x:" . $x . "<br>"; } } It's not looping the way I want, like it just loops once each. Is using for the wrong method or do I need to use while in this case? Quote Link to comment https://forums.phpfreaks.com/topic/179874-multiple-looper/ Share on other sites More sharing options...
[email protected] Posted November 1, 2009 Share Posted November 1, 2009 I used the loops like this recently and worked fine for me... Again it depends on your logic of expected output, let us know ur code in detail //my code for($y=0; $y <= $counter1; $y++) { for($z=0; $z <= $counter2; $z++) { ..} } Quote Link to comment https://forums.phpfreaks.com/topic/179874-multiple-looper/#findComment-948889 Share on other sites More sharing options...
cags Posted November 1, 2009 Share Posted November 1, 2009 $row_count = 4; $col_count = 4; for($row = 0; $row < $row_count; $row++) { for($col = 0; $col < $col_count; $col++) { echo 'x'; } echo '<br/>'; } Quote Link to comment https://forums.phpfreaks.com/topic/179874-multiple-looper/#findComment-948891 Share on other sites More sharing options...
Daniel0 Posted November 1, 2009 Share Posted November 1, 2009 Is using for the wrong method or do I need to use while in this case? Generally, any for loop will have the following structure: for (startingStatement; condition; iterationStatement) { body; } That can be written like this using a while loop: startingStatement; while (condition) { body; iterationStatement; } So it will never really matter if you use a while loop instead of a for loop. It's just two ways of writing the same thing. Thought it might be useful to know. Actually, in a for loop, all the things within the parentheses are optional, so you can also do this: for (; { something; } Quote Link to comment https://forums.phpfreaks.com/topic/179874-multiple-looper/#findComment-948899 Share on other sites More sharing options...
redarrow Posted November 1, 2009 Share Posted November 1, 2009 Are you a new programmer, and asking what all these loops for, and why are there so many. as you been told, does not matter what loop you use, but as long as you no, there other loops for php. for loop. while loop. do while loop. as you get better at coding your start to use all the loops as possable.... most common loop is the FOR loop my opinion There also the foreach array loop out there.... Quote Link to comment https://forums.phpfreaks.com/topic/179874-multiple-looper/#findComment-948903 Share on other sites More sharing options...
Daniel0 Posted November 1, 2009 Share Posted November 1, 2009 as you been told, does not matter what loop you use Not necessarily. Iterating over an associate array would look ugly using e.g. a while loop. Assuming you have the following array: $array = array( 'foo' => 'bar', 'test' => 'hello', ); Iterating over it using a while loop would look something like this: $arrayKeys = array_keys($array); $arrayValues = array_values($array); $i = 0; $arraySize = sizeof($array); while ($i < $arraySize) { echo $arrayKeys[$i] . ' => ' . $arrayValues[$i] . PHP_EOL; ++$i; } Compare that to the following (which is much easier to read): foreach ($array as $key => $value) { echo $key . ' => ' . $value . PHP_EOL; } Another example would be creating an infinite loop (that you of course break out of somehow within the body). Using a while loop you would do this: while (true) { doSomething; } and the for loop equivalent would look like this: for (; { doSomething; } The for loop in this case would be most efficient (though not by much) because it doesn't need to evaluate a statement all the time. Quote Link to comment https://forums.phpfreaks.com/topic/179874-multiple-looper/#findComment-948911 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.