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.
Sofia Petsoulakis681 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.
<?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>
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.
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