Advertisement

Code for the Uncoded: Foreach Loops

Posted on Tuesday, November 10th, 2009 at 1:08 pm

Getting started with any type of language can be difficult for some people. Most tutorials or examples use a lot of “developer terms” that a designer or beginner won’t be familiar with. This can add another level of difficulty to otherwise simple tasks. This is hopefully where Code for the Uncoded comes in.

Learning something new is always easier when it’s put into real world terms. In this example we are going to use the foreach loop to tell the user is a store is currently open, or closed.

Filling the Store

To start, we are going to create a simple array of stores. We are going to name it stores.

When working with foreach, as well as arrays, I like to keep the names plural. It makes it easier to scan the code once you reach hundreds of lines.


<pre>

<?php

$stores = array('My Shoes','My Shirts','My Hats','My Pants');

?>

</pre>

We’ve wrapped our entire script in <pre> tags so that we don’t have to render out <p> tags to format our code. Instead we can just use /n to create a new line, but this will come in the next example.

Now that we have our stores array we can loop out the name of each store using our foreach loop.

In a nutshell the foreach loop is taking each array item (store name) and executing some code that we set. For each store we are going to show the user the name of the store.

foreach($stores as $store){

echo "$store \n";

}

What’s going on?

We told PHP that foreach $stores item (our stores) we want to create a new variable of $store which holds the name of our store. Inside the foreach loop is the code that we want to execute for each store. In this example we are echoing out the value of $store which currently holds our store name. If you ran this script right now you should see something like:

My Shoes
My Shirts
My Hats
My Pants

Open 24/7

Now we can make some quick changes to our array and foreach loop to tell the user if the store is open or closed.

$stores = array(
'My Shoes' => 'Open',
'My Shirts' => 'Closed',
'My Hats' => 'Open',
'My Pants' => 'Open'
);

We’ve modified our $stores array and added a new key to each array item. All this is doing is saying My Shoes holds a value of Open. We can now make 2 changes to our foreach loop to reflect these changes.

foreach($stores as $store => $status){

echo "$store is currently $status \n";

}

*Updated foreach loop. Thanks to Mark Skilbeck

We’ve added a $status variable to our foreach loop to pass our value of either Open or Close to the $status variable. We can now echo out the value of $status to display the current status of our store. Run the script and it should look like:

My Shoes is currently Open
My Shirts is currently Closed
My Hats is currently Open
My Pants is currently Open

final_example

To wrap everything up, the foreach loop takes our array ($stores) and shows the user the name ($store) and status ($status) of each store in $stores. This can be extremely useful when you have a large list of items that need to be shown to the user, or formatted in some way. The possibilities are endless!

Share these links, you kinda have to!
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Twitter

User Comments

  1. Mark Skilbeck
    11/10/09 @ 01:11 pm

    Regarding the line foreach($stores as $store as $status){ – I think you see the typo :)

  2. Matt Vickers
    11/10/09 @ 01:11 pm

    Thanks for pointing that out, I’ve edited the code!

    Such a simple typo would’ve been disastrous to a beginner :P