Advertisement:

Author Topic: PHP Lesson 08 - control structure, foreach loop  (Read 23756 times)

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
PHP Lesson 08 - control structure, foreach loop
« on: September 02, 2003, 01:59:23 AM »
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:

Code: [Select]
$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:

Code: [Select]
$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:

Code: [Select]
$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.
« Last Edit: March 29, 2004, 05:04:42 AM by [Unknown] »

Offline Acf

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,967
  • Gender: Male
  • 101101010112
Re:PHP Lesson 8
« Reply #1 on: September 02, 2003, 11:59:11 AM »
What does iterate mean?? (Sorry i have never heart of the word before)
Sigh...

Offline Joshua Dickerson

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 12,778
  • Gender: Male
    • joshuaadickerson on GitHub
    • joshuaadickerson on LinkedIn
Re:PHP Lesson 8
« Reply #2 on: September 02, 2003, 12:31:26 PM »
go through each one
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?

Offline Acf

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,967
  • Gender: Male
  • 101101010112
Re:PHP Lesson 8
« Reply #3 on: September 02, 2003, 12:33:03 PM »
Sigh...

Offline adam

  • Semi-Newbie
  • *
  • Posts: 97
  • Gender: Male
    • The Daily Blogger
Re: PHP Lesson 08 - control structure, foreach loop
« Reply #4 on: October 17, 2004, 08:16:08 PM »
For some reason I can never understand loops in php despite how great they are like Parhams.  :):(

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: PHP Lesson 08 - control structure, foreach loop
« Reply #5 on: October 17, 2004, 09:18:39 PM »
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]

Offline absolute beginner

  • Newbie
  • *
  • Posts: 2
Re: PHP Lesson 08 - control structure, foreach loop
« Reply #6 on: June 10, 2006, 04:32:56 PM »
I don't know whay I'm doing wrong here...
some wml code... then
Code: [Select]
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...
Code: [Select]
</wml>What is problem here?

Offline JayBachatero

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 19,562
  • Gender: Male
    • @jaycreations on Twitter
    • JayBachatero.com
Re: PHP Lesson 08 - control structure, foreach loop
« Reply #7 on: June 14, 2006, 11:51:48 PM »
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