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 Listing Inventory Items Even More Excitement With Arrays

Brittany Drollinger
Brittany Drollinger
6,160 Points

Quiz Question - Possible Typo

I believe there is a typo regarding one of the quiz questions.

It begins with:
I want to output this: 19801985

<?php
$movies[] = array(
        "title" => "The Empire Strikes Back",
        "year" => 1980
    );

$movies[] = array(
        "title" => "Back to the Future",
        "year" => 1985
    );

foreach($movies as ????) {
    echo $item["year"];
}

?>

And gives the following as possible answers:
A. There's no variable name that would produce this output.
B. $movies
C. $movie
D. $item

I selected A rather than D thinking it was a trick question, because even if you used $item for the value in the foreach loop, you'd never get the output of '19801985.'

EDIT: I realized now I wasn't paying enough attention to the output value as a combination of two years. Silly me.

3 Answers

Hi

The foreach loop takes the movies array, then with each iteration the next element is assigned to ????. In this case $item

So in this instance the first loop/iteration takes 'The Empire strikes back' and defines it as $item echoing out the "year".

The next iteration then takes 'Back to the Future' and defines it as $item similarly echoing out the "year".

This is done without adding any spaces etc resulting in 19801985.

bit of light reading - may give a better explanation

Hope this explains it

Bob

Brittany Drollinger
Brittany Drollinger
6,160 Points

Great explanation. I definitely just didn't pay enough attention, and can't delete my post now that I caught my mistake. But thanks for your answer. =)