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 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,060 Points

thanks very much