Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Working With Functions Displaying Only Four Shirts

Explain me the displaying four shirts in layman terms

I am no expert and trying hard to wrap my head around php and i am stuck on this video. Can someone tell me in layman or easy example of how to display 4 items out of lets say 10, in layman terms taking example from this video?

1 Answer

Ryan Field
Ryan Field
Courses Plus Student 21,241 Points

It's pretty much how he described in the video.

All he's doing is keeping track of how many shirts have been looped through, subtracting that from the total number (which is retrieved at the beginning of the loop) and then starting to display them when the variable keeping track of the remaining shirts is less than four (or whatever number you want to display).

Good enough for logic but i would need some example in easy terms.

Mark VonGyer
Mark VonGyer
21,215 Points

There isn't really much else to add to what Ryan has already said.

What you need are counter variables that keep track of what is going on. suppose you have ten apples. I have a counter of how many apples I currently have - this starts at 10. I give you apples, 1 at a time. I have a counter of how many apples I gave you - this starts at 0. I have x amount of apples left(where x is equal to my starting count - how many apples I gave you).

Therefore I have 4 apples left when my X count = 4. When this happens you have 6 apples. The sum of 4 +6 =10.

You can utilise the traditional for loop to do this, but creating the counters are better.

Once you have 4 apples left, additional code will run that will display the apples.