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

STUCK ON TASK 5 OF 7 FOR ASSOCIATIVE ARRAYS

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

what i have is i created the array $movie = array();

then i created the elements within the array $movie =array("title"=>"Empire Strikes Back", "year" => "1980"; )

so what i need to do is echo out the elements which i pass the first stage which is just adding the title variable: <h1><?php echo $movie["title"];?> (1985)</h1>

step two is adding the year element in the array, i have tried everything from concatenation of the parenthesis and adding the year to creating two separate php blocks . my problem is that i keep getting the error that my exercise 3 doesn't seem to pass.

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

ANY SUGGESTIONS WOULD BE GREAT. I SEEM TO BE STUCK ON THIS FOR COUPLE DAYS NOW.

thanks

23 Answers

Hey Randy,

Good News, apparently something worked. I just copied and pasted my #2 option of using

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

and it decided to pass, go figure.

anyway thanks for the help and for the great tutorials!

This worked for me if anyone is still struggling with this challenge. Thanks, Justin.

Craig Sainsbury
Craig Sainsbury
19,059 Points

This is what i used to pass

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

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

Breaking it down like this:

There is 2 blocks of php code inside the h1 tags

1: <?php echo $movie["title"]; ?> 2: ( <?php echo $movie["year"]; ?> ) This bit of code is inside the parentheses.

Hope this helps

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Could you paste the whole code block in here? What you have looks more or less correct. (Either approach should work. In #1 you left out a space between the title of the movie and the parenthesis, but #2 looks correct.) The only thing I notice from these snippets of code is that the name of the movie should have "The" at the beginning, like this:

$movie["title"] = "The Empire Strikes Back";

I'm not sure if that's the problem or if you just accidentally omitted it when typing it here. If that is the problem, then step 3 should have given you a helpful hint about that. If that's not the problem, please paste the full code block here so I can take a look. It sounds like something minor is off.

Hey Randy thanks for the attempt, i was pretty sure my code was correct. the only thing i can think of is it can be as simple as a common syntax error. I do realize that i forgot a "The", although as you stated this was just a typo when typing on here. The error i get either seems to be A.) i get an error saying task 4 is no longer passing. or B.) Bummer Try again.

THIS IS THE TASK AT HAND:

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

CODE

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

<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>

Now what i do is what i had discussed earlier with #1 and #2 trying to both concatenate the array or by not replacing the parenthesis and instead creating 2 separate php blocks.

I hope this helps clarify the issue I'm facing, if you could help me figure this out, would be much appreciated.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Excellent. Glad you got it working, and I'm glad you are enjoying the tutorials! :~)

I have similar problems with this code here is my code:

<?php

$movie["title"] = "The Empire Strikes Back"; $movie["year"] = 1980; ?> <h1><?php echo $movie["title"]; ?> (<?php echo $movie["year"]; ?>) </h1>

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

The code challenge is being a little too strict! You have added an extra space between the closing parenthesis and the closing H1 tag. It doesn't matter, but the code challenge isn't getting exactly what expects. Sorry about that!

I'll fix it shortly ... but in the meantime, you can change this line --

... echo $movie["year"]; ?>) </h1>

-- to this --

... echo $movie["year"]; ?>)</h1>

-- without a space before the closing H1 tag.

Does that help?

Yep! figured it out Thank you so much! before you know it Mark Zuckerberg will be giving me a call soon?

Just Kidd'in! LOL

This was a very demanding challenge! After hours of working on it, I finally got through it with this thread and other outside resources.

It still doesn't like the closing h1 tag on a fresh line. Removing the spaces between the h1 tags and the php code works....

Hi - I have been trying various ways to get this task to work, but nothing is working at all.

I had already tried the ways listed in various forum listings on here before coming for help - and I see that what I have already tried, is listed here as correct. Here is what I have at the moment and all i'm getting is Bummer! Try again!

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

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

I am guessing that there is probably a typo in there somewhere but my eyes have gone square from trying to get it to pass! lol

Thanks in advance for any help!

lol found the problem (duplicate text), typed it all out again and it worked...thanks anyway :)

Thanks Randy!

Is there any news on when the php & MySql series will start?

Cheers :-)

awesome! thanks for the link :)

Regarding quotation marks:

What is the correct protocol here? I got task 5 to pass without them, but it seems like most examples here use them as listed below:

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

Are they really necessary in the case of movie title and year? I am hoping to program for pay at some point and want to know if I would be viewed as an amateur if I leave the quotation marks out.

My code do not pass and I can not figure out what's wrong with it.

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

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

I know know this thread is old but im having the save problem !! this is my code

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

Adrian Pineda
Adrian Pineda
13,778 Points

Can someone please tell me what I am doing wrong here? <?php $movie = array(); $movie["title"] = "The Empire Strikes Back"; $movie["year"] = 1980; ?> <h1><?php echo $movie["title"];?>(<?php echo $movie["year"]; ?>)</h1>

Thanks!

Bezalel Shikli
Bezalel Shikli
6,976 Points

Hi this is the way I've got the code to work:

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

Sebastian Pikand
Sebastian Pikand
13,987 Points

THIS IS REFICULOUS!!!

<h1><?php echo $movie["title"] . " " . $movie["year"];?></h1> LIKE THIS IT WORKED

<h1> <?php echo $movie["title"] . " " . $movie["year"]; ?> </h1>

LIKE THIS IT DID NOT.... WHAT THE ACTUAL...?

Sebastian Pikand
Sebastian Pikand
13,987 Points

just to be clear, I do understand the whole mistake is within your code checking system but maaaan what the hell???

This worked, but I feel it's not right.

<?php

$movie = [

"title" => "The Empire Strikes Back",

"year" => 1980,

];

?>

<h1><?php echo $movie["title"] . $movie["year"]; ?></h1>