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 Basic PHP Website (2018) Listing and Sorting Inventory Items Associative Arrays

Cliff Jackson
Cliff Jackson
2,887 Points

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

index.php
<?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
Øyvind Andreassen
16,840 Points

You 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
Cliff Jackson
2,887 Points

The code will still not pass in the challenge, have done it like you have written but still an error?

Øyvind Andreassen
Øyvind Andreassen
16,840 Points

The code passes here, but I see that I included an punctuation mark in the title that might cause it to fail.

Cliff Jackson
Cliff Jackson
2,887 Points

Thanks 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
Øyvind Andreassen
16,840 Points

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