Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Listing Inventory Items Associative Arrays

Sofia Petsoulakis
Sofia Petsoulakis
681 Points

Having trouble adding movie["year"] in parenthesis. Tried multiple ways, no sure what I'm doing wrong.

I keep getting errors, I even used a separate php command for the year and tried concatenated the parenthesis with the year. Any help would be appreciated so I can move forward. Thanks.

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



);



?>


<h1><?php echo $movie["title"]($movie["year"];) ?></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>

I know the answer was "The Empire Strikes Back (1980)" but this way you won't learn something. So I posted just a hint for you and with some thinking you would have figured it out ;) Just saying because of the comment you wrote and now is deleted.

2 Answers

Hi Sofia,

You have a few different ways you can handle this.

If you go with separate php blocks then you want to make sure that the parentheses are outside of the php blocks and that you have maintained spacing.

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

Or you could do it with one php block and string concatenation.

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

I'd say the first one is probably more readable.

I would say the concatenation is more readable but perhaps just because i'm used to it :D

You must use string concatenation and that works like this:

<?php
    echo $movie["title"] . $movie["year"] ;
    // output : "The Empire Strikes Back1980"
?>

For getting in the parentheses you just have to add more strings for concatenation:

<?php
    echo $movie["title"] . ' (' . $movie["year"]  . ')';
    // output : "The Empire Strikes Back 1980"
?>

Well I wanted you to think about the actual solution yourself but that doesn't matter now since jason posted the full solution so I edited this as well

Damm this code tags, well now I've got it xD