• Welcome to Simple Machines Community Forum. Please login or sign up.
January 20, 2022, 05:13:58 AM

News:

SMF 2.1 RC4 has been released! Try it out and help us test! :) Read more.


PHP Lesson 08 - control structure, foreach loop

Started by Parham, September 02, 2003, 01:59:23 AM

Previous topic - Next topic

Parham

September 02, 2003, 01:59:23 AM Last Edit: March 29, 2004, 05:04:42 AM by [Unknown]
The second type of control structure we're going to discuss is the "foreach" loop.  The "foreach" loop allows you to iterate over each value of an array.  The "foreach" loop has two syntaxes, one for arrays you might have initialized, and one more specifically for associated arrays which you might have initialized.  Here are the syntaxes for both:

foreach (array_expression as $value) { do something with $value }
foreach (array_expression as $key => $value) { do something with $key and $value }

Let's take our example from lesson 5:


$people = array('Parham','Jeff','Joseph','Unknown','David','Alex');


As you can see, we've declared an array called $people which store the names of a few people.  While you can get access each element using $people[0 to 5] (as we discussed in our array lesson), if you didn't know how big the array was, you'd never know how far to go.  There is a combination of a loop and a function you could use, but for now, this is the SIMPLEST way to iterate through an entire array.  Let's use the above array and the above "foreach" loop syntax to write out a simple example:


$people = array('Parham','Jeff','Joseph','Unknown','David','Alex');
foreach ($people as $person) { //note the singular and plural variable names
  echo "$person\n";
}


The above "foreach" loop says the following:  "foreach element (person) in the array (people), print the element (person's name)".  The example above will have the following output:

"Parham
Jeff
Joseph
Unknown
David
Alex"

Of course you don't just have to print out each value in the array.  There are endless possibilities of what you can do with each value in the array.  I just think that printing each element is the easiest and therefore will show you the most clearly how to use a "foreach" loop.  Let's do the same thing with the associated array syntax:


$people = array('person1' => 'Parham','person2' => 'Jeff','person3' => 'Joseph','person4' => 'Unknown','person5' => 'David','person6' => 'Alex');
foreach ($people as $person => $name) { //again note the singular and plural variables names
  echo "$person - $name\n";
}


The example above will have the following output:

"person1 - Parham
person2 - Jeff
person3 - Joseph
person4 - Unknown
person5 - David
person6 - Alex"

That's about it for the "foreach" loop.  So you might ask why is the "foreach" loop called a control structure.  Well that's because PHP will run the block of code as many times as there are elements in the array.  It will execute the block, move back up, and execute again until all the elements are dealt with.

That's about it for the "foreach" loop.  Hopefully those of you reading the tutorials aren't finding these hard or complicated to understand.  The lessons at this point might seem a little like they're all over the place, but by the time the main lessons are complete, I'll demonstrate putting all these lessons together to create simple programs.  For those of you that really want to learn, expect homework after the main lessons are done.  The homework won't be at all complicated or tricky, but it will help you understand some of PHP's features better.  I WILL check all programs you guys write so I know you're on the right track.

Acf

What does iterate mean?? (Sorry i have never heart of the word before)
Sigh...

Joshua Dickerson

Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Acf

Sigh...

adam

For some reason I can never understand loops in php despite how great they are like Parhams.  :):(

[Unknown]

A loop is a simple "construct" (something built into the language and not possible to make custom, unlike a function) that simply does something more than once.

The difference between for, while, foreach, do, and etc... is just how they limit how many times it's done.... but in the end, it's just doing something more than once.

-[Unknown]

absolute beginner

I don't know whay I'm doing wrong here...
some wml code... then
echo "<select name=\"to\" value=\"".$to."\">";

$names=file('users.txt');

foreach ($names as $name)
{
echo "<option value=\"".$name."\">".$name."</option>";
}
echo "</select>";

some wml code...
</wml>
What is problem here?

JayBachatero

What exactly are you trying to do?  What errors are you getting?
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

Advertisement: