Jump to content

Recommended Posts

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.

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.

 

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

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

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.

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

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.

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  :rtfm: was born :)

 

good post indeed.  ;D

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.