Simple task list frontend rewrite I was recently going through some code that I wrote a while ago as an effort to improve my code.

There was one foreach statement that I was especially proud of the rewrite as I believe it is much more readable than it was before, and I believe it will last longer now.

This code in particular is simply a table body that has a check box (Basically a status check) and a plaintext "task" that was assigned to users stored in a database.

Here is the old code:

&lt;?
    $taskuid = $tasks['uid'];
    $type    = "task";
    foreach($taskData as $key =&gt; $value) {
        echo "&lt;tr style='" . (($value['status'] == 1) ? "background-color:green;text-decoration:line-through;" : "") . "'&gt;";
        echo "&lt;td onclick='toggleCheck($key, $taskuid, \"$type\")' style='vertical-align:middle;cursor:pointer;'&gt; &lt;center&gt; " . (($value['status'] == 1) ? "&lt;i data-status='$value[status]' id='$key-task' class='fa fa-lg fa-check-circle-o' aria-hidden='true'&gt;&lt;/i&gt;" : //Status complete
            "&lt;i data-status='$value[status]' id='$key-task' class='fa fa-lg fa-circle-o' aria-hidden='true'&gt;&lt;/i&gt;") . " &lt;/center&gt; &lt;/td&gt;"; //Status incomplete
        echo "&lt;td class='unselectable'&gt;$value[task]&lt;/td&gt;";
        echo "&lt;/tr&gt;";
    }
?&gt;


Here is the new code:

&lt;?
    foreach($taskData as $key =&gt; $value) {
        echo "&lt;tr class='" . (($value['status'] == 1) ? "checkedTR" : "") . "'&gt;";
        echo "  &lt;td onclick='toggleCheck($key, $tasks[uid], \"task\")' valign='middle' class='clickable'&gt;";
        echo "      &lt;center&gt;";
        echo "          &lt;i data-status='$value[status]' id='$key-task' class='fa fa-lg fa-" . (($value['status']) ? "check-circle-o" : "circle-o") . "' aria-hidden='true'&gt;&lt;/i&gt;";
        echo "      &lt;/center&gt;";
        echo "  &lt;td class='unselectable'&gt;$value[task]&lt;/td&gt;";
        echo "&lt;/tr&gt;";
    }
?&gt;


While writing this, I realized the code actually has the same amount of lines, but there is (what I consider to be) a significant difference in the amount of symbols used (The old code being 752 characters and the new one being 564 characters That's nearly 200 characters!)

My question is this, what would you have done differently while rewriting this code? I'm very interested in the different ways code can be written, I know there are a lot of ways this simple task could be done.
Consider 

&lt;?php
    foreach ($taskData as $key =&gt; $value) {
        $class = ($value['status']) ? 'check-' : '';
        $trClass = ($value['status'] == 1) ? ' class="checkedTR"' : '';
        echo &lt;&lt;&lt;EOHTML
&lt;tr$trClass&gt;
  &lt;td onclick="toggleCheck($key, {$tasks['uid']}, 'task')" valign="middle" class="clickable"&gt;
    &lt;center&gt;
      &lt;i data-status="{$value['status']}" id="$key-task" class="fa fa-lg fa-{$class}circle-o" aria-hidden="true"&gt;&lt;/i&gt;
    &lt;/center&gt;
  &lt;/td&gt;
  &lt;td class="unselectable"&gt;{$value['task']}&lt;/td&gt;
&lt;/tr&gt;
EOHTML;
    }
?&gt;


I prefer the long versions of tags.  So no &lt;? or &lt;=.  It may not matter as much now, but it used to be that some sites would turn off the short versions.  So software could suddenly stop working for no apparent reason.  

I prefer to use single quotes in PHP unless I have to use double quotes, e.g. for variable interpolation.  This way I can see at a glance that a string is not using variable interpolation.  

If I'm working with a block of lines in PHP, I prefer to use Heredocs and Nowdocs.  I prefer Nowdoc, but it won't work here.  We need the variable interpolation.  The original code will put the entire thing on one line.  This version will display it more like it looks here.  

I prefer to use double quotes in HTML.  No particular reason except that it looks better to me.  

I prefer to avoid the $tasks[uid] syntax.  Outside of a string, it's wrong and unreliable.  Inside of a string it's reliable, but it can be misleading.  People see it working inside of a string and don't know why the behavior is special.  Also, if people copy something like $tasks[FIELD] into a string, it may not be obvious why it doesn't work the same way inside a string as outside.  Always using the curly braces is clearer in my opinion.  

In this case, I would pick the Javascript quoting to avoid escaping, i.e. single quotes.  

Your HTML indentation is inconsistent.  Sometimes two spaces and sometimes four.  

You lost a closing &lt;/td&gt; in the refactor.