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 Displaying Only Four Shirts

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Displaying only four shirts

Hello,

Question 1:

So I am confuse in 2 sections of the code:

1) If value is < 4 wouldnt that = 3. Less than 4 is 3?? How does this work exactly.

I would of written :

if($total_products - $position == 4) if($total_products - $position <4)

What is the difference exactly between these 2 scenarios.

Question 2:

I don't exactly understand why the variable $list_view_html was added at all.

All I see is an empty string variable that was concatenated with something that was already being echoed??

Can someone help me please understand.

Thank you,

Giovanni

4 Answers

Mark McGuinness
PLUS
Mark McGuinness
Courses Plus Student 26,312 Points

Hey Giovanni

What he's doing is echoing out the contents of the $list_view_html variable, (i.e. displaying the last 4 shirts in the browser window) after the loop has built up and joined together (concatenated) the html for the last 4 shirts and put this into the $list_view_html variable, which was initially empty.

Hope that helps :)

Andrew Dushane
Andrew Dushane
9,264 Points

1: The loop is counting 0, 1, 2, 3 so when you get to 3, you've gone through it 4 times. Hence the < 4.

2: the $list_view_html variable gets a product added to it each time the loop goes through. So it starts empty, gets the first shirt on the first loop, etc.

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Hi Thank you for answering.

The loop count now makes perfect sense.

The 2nd question doesn't yet Im still confused.

Wouldn't echoing the function get_list_view_html still loop through all the 4 shirts.

What I see in the video is that he wants to echo the function outside the loop what is the purpose of this?

Thanks,

Giovanni