Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Working With Functions Creating the Shirt Display Function

Steven Griffiths
Steven Griffiths
3,165 Points

Please explain step by step the get_list_view_html function and how it works in conjunction with the foreach loop

Please can someone go through this with me step by step?

Particularly in relation to how the function is working with the foreach loop and the "why's and how's" regarding the variables in the argument.

php
function get_list_view_html($product_id, $product){
//build html output
return $output;
}

The video is very brief and it's not helping even though I've now watched it 10 times!!

There are a couple of other questions related to what I'm asking but with no "Official" response.

Thanks in advance

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

Ok, so:

<?php 
        foreach($products as $product_id => $product) { 
        echo get_list_view_html($product_id,$product);
        }
?>

loops through all the products in the $products array one at a time and assigns it to two working/temporary variables, $product_id and $product. That's what this does:

as $product_id => $product

The "as" keyword tells PHP to assign the current index of the array to the $product_id, and the "=>" tell's PHP to assign the array stored at the current index to $product. When I write loops like this I always think:

foreach( $item_in_an_array as $item_index => $item_value)

That way I don't confuse things, because you can also write a foreach loop like this:

foreach( $item_in_an_array as $item_value)

if you don't really care what the $value's position in the array is.

Now, in programming, doesn't really matter the language, the code inside the loop is executed every time the loop runs. So if you are loop through an array with ten items, the code inside the loop will run ten times. In this case, the code inside the loop is:

echo get_list_view_html($product_id,$product);

So every time the loop runs it echoes the value of the get_list_view_html() function. It's important to note that computers read code from left to right, but execute code from right to left. This means that get_list_view_html is executed before echo is executed.

The get_list_view_html() function takes two arguments, $product_id and $product, which correspond to the working variables the foreach loop uses as temporarily store the index and value of the current item in the loop. When these values are passed inside of the function, they are used to generate the html to display each product.

Now for the "why and how" of the functions arguments. The "why" of adding arguments to a function is to ensure that data needed inside the function will be available to the function. PHP does this by throwing a fatal error if a function defined with arguments:

function cat_in_the_hat( $thing1, $thing2){
    // some code that causes mayhem.
}

is called without any arguments supplied:

// no mayhem will be caused because
// cat_in_the_hat() doesn't have $thing1 and $thing2
cat_in_the_hat();

In regards to the product code, Randy decided to make sure that product information had to be given to the function so that it could generate the html for the product.

The "how" arguments work in a function is similar to the way arguments in a foreach loop work. When a function is defined with arguments:

function get_list_view_html($product_id, $product) {
    // code to generate html
}

those arguments are used as temporary variables when the function is executed. In this case, the arguments are used to temporarily hold the $product_id and $product info while being used inside the function to generate parts of the html like the link's href:

'<a href="shirt.php?id=' . $product_id . '">

Hopefully, this will clear some things up for you. If you need anything else cleared up just let me know.

Steven Griffiths
Steven Griffiths
3,165 Points

Thanks Andrew for taking so much time and effort to explain all that, I very much appreciate it :-)

I'm just trying to digest it all now. I may have another question coming up!

Steven Griffiths
Steven Griffiths
3,165 Points

OK, I think I’ve got it. Let me explain back to you in my own words and in the order that makes sense to me. Please can you give me some feedback if I’ve misunderstood anything?

In shirts.php the foreach loop begins and looks in the $products array and stores the first item β€œ101” inside $product_id. It then stores all the other values from β€œ101” in $product including the β€œname”, "price”,” description”, etc.

Now we load the function called get_list_view_html with those same variables ($product_id and $product) as its arguments. In other words we give the function all the values it needs from the first item in the array.

PHP then finds the function get_list_view_html in the products.php file and passes the values of $product_id and $product into it.

It then sets an empty variable of $output and then stores the first line of HTML in it, concatenating with the $product_id (101) then moves on to the next line and does the same with $product[β€œname”] and so on, until the variable $output contains all the html needed to display everything for the first t-shirt.

return $output; returns all that html back to the function where it was originally called on the shirts.php page where it is echoed out.

It then goes back to the beginning of the loop moving down to the next item in the $products array and repeats the process until it reaches the end of the array.

Andrew Shook
Andrew Shook
31,709 Points

Yep, that's it in a nutshell.