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

Cheng Cgc
Cheng Cgc
7,192 Points

What Wrong With My Code?

is there anything wrong with my coding...?

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

1 Answer

-- --
-- --
12,382 Points

Hi Cheng,

First, create the (associative) array $movie, which stores the key "title" with the value "The Empire Strikes Back". You could do that like so:

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

Adding more items to your array can simply be done like this (you'll have to do this later during the challenge):

<?php 
$movie = array("title" => "The Empire Strikes Back", "example" => "Value of Example");   
?>

Now that you have the the movie title stored within your array, you should replace the current movie title within the h1 tags with the movie title stored in the array, whilst leaving the year in tact. Remove the current title within the h1 tags and echo the value of "title" right there instead.

You could do so like this:

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

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

<table>
...

This code will display "The Empire Strikes Back (1985)" as the h1.

Another option would be this (which I assume is the way you tried to complete the exercise):

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

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

I hope this helps! :)