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 trialCliff Jackson
2,887 PointsCant get title to output from array?
Trying to get the title to echo out from the array but will not work, any ideas what i am missing?
<?php echo "<h1>$movie ["title"](1985)</h1>";?>
<?php
$movie = array(
"title" => "The Empire Strikes Back."
);
?>
<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>
2 Answers
Øyvind Andreassen
16,839 PointsYou have three issues that are preventing you from getting the result you want.
First you're trying to access a variable that isn't set yet. So the $movie array has to be set before the echo statement. Secondly you have a space between the array-variable and the key. Third, you can't mix HTML and PHP that way, but you can solve it by concatenating elements together.
Code below should work.
<?php
$movie = array(
"title" => "The Empire Strikes Back."
);
?>
<?php echo "<h1>" . $movie["title"] . "(1985)</h1>";?>
Cliff Jackson
2,887 PointsThanks oyvynd , I have actually done this challenge before but paused my subs for two months so I’m trying to refresh. I missed the concatenation off which i couldn’t remember. But the question does not specify about setting the variable first which does not help. One of the problems with treehouse is the wording on challenges which many people struggle with and one of the reasons I paused. Thanks for the assistance!
Øyvind Andreassen
16,839 PointsCliff Jackson, no problem. The question doesn't specify it but I guess they are assuming that if you got this far in the PHP courses you should know this. Also, hit one up on my answer so this question get's marked as solved.
Cliff Jackson
2,887 PointsCliff Jackson
2,887 PointsThe code will still not pass in the challenge, have done it like you have written but still an error?
Øyvind Andreassen
16,839 PointsØyvind Andreassen
16,839 PointsThe code passes here, but I see that I included an punctuation mark in the title that might cause it to fail.