Ruko Posted September 8, 2010 Share Posted September 8, 2010 Hello, I dont really understand the for and while loops. And I have bad vocabulary so, can explain these two things in plain english. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/ Share on other sites More sharing options...
petroz Posted September 8, 2010 Share Posted September 8, 2010 Foreach is good for looping through arrays, while is good for database work. See link below.. http://www.webhostingtalk.com/showthread.php?t=519883 Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108503 Share on other sites More sharing options...
HaLo2FrEeEk Posted September 8, 2010 Share Posted September 8, 2010 I've always thought of for loops as more defined while loops. Here is an example: for($i = 1; $i <= 10; $i++) { echo "This is line " . $i . "<br>\n"; } In plain English, here is what's happening: The variable $i is defined with a value of 1 The loop is set to run while the value of $i is less than or equal to 10 (so it'll run 10 times) The loop is instructed to increase the value of $i after each iteration (each time it runs the loop) The code executes, printing 10 lines reading "This is line 1"..."This is line 10" The loop exits. The same can be done with a while loop: $i = 1; while($i <= 10) { echo "This is line " . $i . "<br>\n"; $i++; } Almost the exact same thing is happening here, only in a different order: $i is defined with a value of 1 The loop is instructed to continue running, as long as the value of $i is less than or equal to 10 (so it'll run 10 times) The first line executes, printing the line "This is line 1" The second line executes, increasing the value of $i to 2 The loop checks that the value of $i is still less than or equal to 10, if it is, it prints the next line The loop continues until the line "This is line 10" prints The value of $i is increased to 11 The loop exits since the value of $i (11) is now greater than 10. Foreach loops are probably my favorite type of loop. They're great for working with arrays: $array = array("Hello" => "World"); foreach($array as $key => $value) { echo "The item with key \"" . $key . "\" has a value of \"" . $value . "\".<br>\n"; } They're also very simple to explain: For each item in the array $array, define $key as that item's key, and define $value as that item's value (ex. ["Hello"] = "World") Using the above example, I would get a line that looked like this: The item with key "Hello" has a value of "World". The loop continues until all items in the array have been processed. Once you understand them, loops are actually quite easy to work with and they REALLY come in handy. If you have any more questions please feel free to let me know and I'll do my best to help you out. Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108566 Share on other sites More sharing options...
JasonLewis Posted September 8, 2010 Share Posted September 8, 2010 It must be noted though that each different kind of loop is useful in many different circumstances, you just need to pick the one that will suit it best. And often the same thing can be achieved far easier by actually looking at what you need to loop over, and how you need to loop over it before you go ahead and do it with your favorite kind of loop. The best explanation can be found on the manual: while for foreach And one that is often overlooked, do-while. Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108571 Share on other sites More sharing options...
rwwd Posted September 8, 2010 Share Posted September 8, 2010 Hi all, While is for looping through database stuff, for is more controlled and can be used dynamically & foreach are brilliant for iterating through array's. Question: I use while loop's for DB work even if there is only 1 row in the DB using fetch_object, I also omit the loop and access the DB directly using fetch_object if there is only 1 row to be returned; is this good or bad practise Just depends on what mood I am in really, but both ways work nicely.. Cheers, Rw Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108581 Share on other sites More sharing options...
trq Posted September 8, 2010 Share Posted September 8, 2010 I use while loop's for DB work even if there is only 1 row in the DB using fetch_object It makes no sense to do so. Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108589 Share on other sites More sharing options...
rwwd Posted September 8, 2010 Share Posted September 8, 2010 Hi there Thorpe, I do different approaches sometimes to see if there is any discernible difference, then I see a logical reason to do something one way, then for something else, use another method. Just my way of working; may not be right; but that's what learning is about Cheers, Rw Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108601 Share on other sites More sharing options...
trq Posted September 8, 2010 Share Posted September 8, 2010 You do understand that constructing a loop for something that doesn't require it is obviously not required though? Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108605 Share on other sites More sharing options...
Alex Posted September 8, 2010 Share Posted September 8, 2010 While is for looping through database stuff.. That's not fair to say at all. While loops can, and are, used for countless different tasks. The way you loop over a set of data using the while() loop in PHP is but one way it's used. Saying that they're for fetching database information alone really isn't accurate. Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108834 Share on other sites More sharing options...
rwwd Posted September 8, 2010 Share Posted September 8, 2010 Hi there AlexD, Saying that they're for fetching database information alone really isn't accurate. Yes, I admit that, but generally that's all I use them for at the moment, I am still learning, so finding different ways of doing things is always a good thing to me. I use C a lot for embedded programming on FPGA's and while loops are invaluable for doing other tasks whilst waiting for other tasks to complete. @thorpe: You do understand that constructing a loop for something that doesn't require it is obviously not required though? Yes, I do understand that, when there is no possible way that the row count will ever exceed 1 I don't use a loop, I just use fetch_object() an call the data directly. I have this method for holding settings for project intranet sites so that I (as admin) can alter things (date format, site title, friendly urls and various other options) dynamically. Otherwise I will use a while loop just in case the row count will ever exceed 1 (which it can & often does) Again, I am still learning, and enjoy finding new ways of doing things that make coding easier/faster to do. Cheers, Rw Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108844 Share on other sites More sharing options...
HaLo2FrEeEk Posted September 8, 2010 Share Posted September 8, 2010 Wow, it's like my very long, very descriptive post detailing examples of use and english translations for each type of loop (except do-while,) just doesn't exist. Sure am glad I took all that time to write it all up. Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108872 Share on other sites More sharing options...
Maq Posted September 8, 2010 Share Posted September 8, 2010 Hello, I dont really understand the for and while loops. And I have bad vocabulary so, can explain these two things in plain english. Thanks Have you done any research? If so, are you stuck on a specific part? These are very primitive concepts of most languages. There are probably millions of tutorials and examples (in plain English) on the web. Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108886 Share on other sites More sharing options...
mikosiko Posted September 8, 2010 Share Posted September 8, 2010 Wow, it's like my very long, very descriptive post detailing examples of use and english translations for each type of loop (except do-while,) just doesn't exist. Sure am glad I took all that time to write it all up. now you know how the acronym RTFM was born good post indeed. Quote Link to comment https://forums.phpfreaks.com/topic/212817-for-and-while-loops-in-plain-english/#findComment-1108893 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.