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 Displaying Item Details

Amit Kumar
Amit Kumar
1,063 Points

why we are using opening brace in php statement and closing brace in php statement can we use is without this

<?php

if(strtolower($item['category']) == 'books'){ ?> <-----

<?php } ?> <---

1 Answer

Missy Kailet
Missy Kailet
4,475 Points

Old question, but I thought I'd attempt to answer since it's still open.

PHP does not parse anything that comes after the closing statement, so the instructor decided to use that approach and type out HTML markup, then echo PHP variables when needed, instead of stuffing everything into echo statements. One may argue it makes for better readability and good separation of code (visual markup vs. server script). Either code below would have worked:

<?php 
      if(strtolower($item["category"]) == "books"){

      echo "<tr><th>Authors</th><td>" .  $item["authors"] . "</td></tr>";
      echo "<tr><th>Publisher</th><td>" . $item["publisher"] . "</td></tr>";
      echo "<tr><th>ISBN</th>";
      echo "<td>". $item["isbn"] . "</td>";
      echo "</tr>";
?>
<?php 
      if(strtolower($item["category"]) == "books"){ ?>
      <tr>
             <th>Authors</th>
             <td><?php echo $item["authors"]; ?></td>
      </tr>
      <tr>
             <th>Publisher</th>
             <td><?php echo $item["publisher"]; ?></td>
      </tr>
      <tr>
             <th>ISBN</th>
             <td><?php echo $item["isbn"]; ?></td>
      </tr>
<?php } ?>
Sam Gord
Sam Gord
14,084 Points

thanks very much