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

Different way of laying it out - FOREACH LOOP Video

In the video, Alena Holligan echo's out the code to display a table shown below:

<?php
include("list.php")
echo '<table>';
echo '<tr>';
echo '<th>Title</th>';
echo '<th>Priority</th>';
echo '<th>Due Date</th>';
echo '<th>Complete</th>';
echo '</tr>';

foreach($list as $item) {
    echo '<tr>';
    echo '<td>' . $item['title'] . '<br />\n';
    echo '<td>' . $item['priority'] . '<br />\n';
    echo '<td>' . $item['due'] . '<br />\n';
    echo '<td>';
        if ($item['complete']) {
            echo 'Yes';
        } else {
            echo 'No';
        }
    echo '</td>\n';
    echo '</tr>';
}

echo '</table>';
?>

Would it not be better to do the following?

<table>
  <tr>
    <th>Title</th>
    <th>Priority</th>
    <th>Due Date</th>
    <th>Complete</th>
  </tr>

  <?php
    include("list.php");
    foreach($list as $item) {
      echo '<tr>';
      echo '<td>' . $item['title'] . '<br />\n';
      echo '<td>' . $item['priority'] . '<br />\n';
      echo '<td>' . $item['due'] . '<br />\n';
      echo '<td>';
          if ($item['complete']) {
              echo 'Yes';
          } else {
              echo 'No';
          }
      echo '</td>\n';
      echo '</tr>';
  }

  ?>

</table>

Is it not repetitive to be repeating out the echo statement every time? Also does it impact on performance of the code? And based on opinion, would you say that the second lot of code is better for readbility purposes, since there are no line breaks utilised in the first set of echos, created by Alena?