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

Error in PHP Associative Arrays Task: "second movie from the array"

There's a small error in the task for

Build a Simple PHP Application Listing Inventory Items Associative Arrays > 3/7

"Right now, the <h1> element has the title of the first movie as a static piece of text. Replace that with a PHP command to display the title of the second movie from the array. (Be sure to leave the <h1> tags, the parentheses, and the year intact.)"

At this point there is only one movie in the array: "The Empire Strikes Back" and the correct solution is

<?php echo $movie["title"]; ?>

16 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Glad you are enjoying the tutorials! I try really hard to write the error messages to give helpful hints in these cases when something is wrong that is not the focus. I'll take a look at this one again and see if there's something I can do to improve the messages. Thanks for the feedback!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Let me describe a common task that PHP developers working at a company will have to complete. They will often get handed a complete HTML file that will need to be made into a dynamic template. They'll often need to turn static pieces of content into PHP variables, much like we've done in this code challenge. They'll have to be very careful to replace just the right thing in that template file, making sure to leave any spaces or HTML class names or pieces of text that need to remain static. If they remove those, the final template file won't look like the original HTML template file.

I have seen countless examples where the developers aren't careful enough and things end up missing in the final version of a site, things that designers were very careful and intentional about including. The person reviewing that site (a client or an internal project owner) then sees the end version of the site and complains that it feels sloppy, careless, and unprofessional because of all these little problems.

So I guess I wouldn't call the parentheses and the year "non relevant". There's a piece of static HTML and part of it needs to be replaced with a PHP variable. It's important as a developer that you are able to replace just the right part ... no more, no less.

Does that make sense?

It gave me a better look about PHP developer! Thank you ! ^^

I am stuck on this code challenge. My code looks like this so far and I can't figure out what is wrong. Can you please help me out.

<?php $movie = array( "title" => "The Empire Strikes Back" ); ?>

<h1> <?php echo $movie["title"]; ?> (1985)</h1>

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

I apologize for the trouble. The issue is that you added a space between the H1 and the title. Instead of this ...

 <h1> <?php echo $movie["title"]; ?> (1985)</h1>

... the code challenge is looking for this ...

<h1><?php echo $movie["title"]; ?> (1985)</h1>

It's completely fine that you have the space there! :-) I need to update the code challenge to be less strict on that point.

Randy, thanks for the quick response!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Sorry for the confusion! I need to rewrite the instructions because a number of people are getting hung up on the "first" and "second" movie references.

Here's what I intended: When the page first loads, it has information about Back to the Future. I refer to that as the "first movie". In the code challenge, we're changing it to show information about "The Empire Strikes Back" instead; I refer to that as the second movie. There should only ever be one movie in the array.

I must admit I got hung up on the second movie in the array and was trying to make the following work:

echo $movie[1]["title"]; 

Given your explanation then all we need is the word "new" in the question to make it clearer to idiots like me who then try to build a multi-dimensional array.

:-)

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

(You are not an idiot. :~) I like "new" instead of "second"; great suggestion! I also like "original" instead of "first."

I have rewritten the instructions to (hopefully) make it clearer that we are removing the information about the original movie and replacing it with information about the new movie. Would you mind taking a look and letting me know if you think it's clearer?

As clear as a bell now. :-)

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Excellent. Thanks for taking a look!

Hi Randy, I'm stuck on the same task : my answer : <h1><?php echo $movie["title"]; ?></h1> but I get an answer saying that, ok the h1 is right but something is missing - and i cannot see what. Could it be this "(Be sure to leave the tags, the parentheses, and the year intact.)" instruction ? btw I do not understand what you mean : should we keep the (1985) from the previous movie ?

Oh and I also wanted to say this series is really great Randy, thanks !!

the message read : "Bummer! It looks like the title of the second movie is in the <h1>, but something else isn't quite right. Double-check the parentheses and the year." Yep yep yep, now i found out : just had a space missing between the closing php tag and the opening parenthesis for the year. In these quizzes I guess it would be fine to concentrate on the point (ie : the php code in the h1) and leave the rest (the non relevant parentheses and year thing), what do you think ?

best, p

Hi Randy,

Thanks for your swift answer - yes, that absolutely makes sense ! "Non relevant" was probably not the right way to describe it. What I meant was only this : when you try the challenge, and if you are new to the subject, if you get stuck at some point you sometimes spend a lot of time trying to figure out what was wrong... In my experience of challenges on various learning sites (Codacademy, etc.), this has always been the problematic point. So, this was more a technical remark. I guess programming challenges must be tricky and obviously more complex than programming quizzes, but I think they should (1) always be focused on the point to learn and (2) flexible as concerns content (if you don't write the exact "title of the movie", well ok...) But once again, thanks for the fantastic tutorial, I am looking very much forward to the next one to come, you are doing a great job !! p

Hi Randy,

Thanks for your swift answer - yes, that absolutely makes sense ! "Non relevant" was probably not the right way to describe it.

What I meant was only this : when you try the challenge, and if you are new to the subject, if you get stuck at some point you sometimes spend a lot of time trying to figure out what was wrong... In my experience of challenges on various learning sites (Codacademy, etc.), this has always been the problematic point. So, this was more a technical remark.

I guess programming challenges must be tricky and obviously more complex than programming quizzes, but I think they should (1) always be focused on the point to learn and (2) flexible as concerns content (if you don't write the exact "title of the movie", well ok...)

But once again, thanks for the fantastic tutorial, I am looking very much forward to the next one to come, you are doing a great job !!

p

Randy Hoyt im stuck, I have entered the syntax the way you put it and it still wont let me pass.

please disregard the post I figured it out.

thanks, you rock!

I'm sorry for necroposting, but I get an error "The title of the second movie is not displayed in the <h1>."

Can anyone give me some help?:)

My code

<h1><?php echo $movie["title"]; ?> (1985)</h1>
<table>
<tr>
<th>Director</th>
<td>Robert Zemeckis</td>
</tr>
<tr>
<th>IMDB Rating</th>
<td>8.5</td>
</tr>
<tr>
<th>IMDB Ranking</th>
<td>53</td>
</tr>
</table>
<?php 
  $movie = array(
    "title" => "The Empire Strikes Back",
  );
?>

Have you tried putting your php block above the html? the way you have it written now the code does not find a value for $movie until the end and by that time you have already passed the h1 tag.

I think this may work.

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

<h1><?php echo $movie ["title"];?>, (<? php echo $movie ["year"]?>)</h1>

try this and see if it works. wish u good luck.