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

I have tried these exercises and rewatched the videos but I'm still getting them wrong. How do i do it?

I am trying to change the film title using an array $movie["title"] = "The Empire Strikes Back". So I write my code as follows;

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

but when I check the exercise, i'm getting it wrong. I have gone back to the videos but they just don't seem to correlate to what the exercise is asking me to do. I need help as I'm becoming increasingly frustrated with what feels like a mismatch between the videos and the exercises.

index.php
<?php
$movie["title"] = "The Empire Strikes Back";
?>
<?php
echo "<h1>"$movie "(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>

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi there.

It looks like you're trying to cocatenate the value of an associative array to the start of another string, so join one thing to the other.

To do this you need the concatenation operator which is a period.

Try the following making sure you reference the array key.

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

So close and yet so far! I don't remember seeing a video on concatenating the array, but it might be one I watched a while back. Thanks for your support, sorted now. :)

Daniel Stopka
Daniel Stopka
13,520 Points

Hi,

you can also leave h1 tag where it is and add php inside it, like this:

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

Thanks Daniel, I started messing around with the code on phpfiddle and sure enough, this was the solution I ended up at. It also seems like the simplest i.e. less code for syntax errors! :P

Thanks for your help.