echoing html tags in php I have the following code below I was wondering if there is a way to clean it up/improve it especially near the if statements. I am new to programming and this is what I came up with and its working fine I just think there might be a better way to accomplish it.

    &lt;?php 

require 'DB.php';

try {

    $stmt = $conn-&gt;prepare('SELECT * FROM equipment');
    $stmt-&gt;execute();
    } catch(PDOException $e){
        echo'ERROR: ' . $e-&gt;getMessage();
    }

echo "&lt;table class='tableTwo'&gt;
&lt;tr&gt;
&lt;th&gt;Equipment Type&lt;/th&gt;
&lt;th&gt;Unit Number&lt;/th&gt;
&lt;th&gt;Last Updated&lt;/th&gt;
&lt;th&gt;Current Hours&lt;/th&gt;
&lt;th&gt;Scheduled PM&lt;/th&gt;
&lt;/tr&gt;";

while($row = $stmt-&gt;fetch())
  {
  echo "&lt;tr&gt;";
  echo "&lt;td&gt;" . $row['equipType'] . "&lt;/td&gt;";
  echo "&lt;td&gt;" . $row['unitNumber'] . "&lt;/td&gt;";
  echo "&lt;td&gt;" . $row['lastUpdate'] . "&lt;/td&gt;";

  if ($row['nextPM'] - $row['currentHours'] &gt; 100) {

  echo "&lt;td class='good'&gt;" . $row['currentHours'] . "&lt;/td&gt;";
  echo "&lt;td class='good'&gt;" . $row['nextPM'] . "&lt;/td&gt;";

  } else if ($row['nextPM'] - $row['currentHours'] &lt; 100 &amp;&amp; $row['nextPM'] - $row['currentHours'] &gt; 50){

  echo "&lt;td class='soon'&gt;" . $row['currentHours'] . "&lt;/td&gt;";
  echo "&lt;td class='soon'&gt;" . $row['nextPM'] . "&lt;/td&gt;";

  }

  else {
  echo "&lt;td class='bad'&gt;" . $row['currentHours'] . "&lt;/td&gt;";
  echo "&lt;td class='bad'&gt;" . $row['nextPM'] . "&lt;/td&gt;";
  }
  echo "&lt;/tr&gt;";
  }
echo "&lt;/table&gt;";
?&gt;
My own personal preference when outputting things like this is to put the portion where data is actually retrieved in a separate file and then use the alternate PHP structure syntax. While this probably isn't everyone's preferred coding style, in my opinion it makes it easier to structure HTML so that its readable among the PHP. From what I have seen, this appears to be relatively common in templates.

Just a quick once-over your file to show you what I mean:

&lt;?php
require 'DB.php'

try {
    $stmt = $conn-&gt;query('SELECT * FROM equipment');
} 
catch(PDOException $e){
    echo'ERROR: ' . $e-&gt;getMessage();
    die();
}

?&gt;
&lt;table class='tableTwo'&gt;
    &lt;tr&gt;
    &lt;th&gt;Equipment Type&lt;/th&gt;
    &lt;th&gt;Unit Number&lt;/th&gt;
    &lt;th&gt;Last Updated&lt;/th&gt;
    &lt;th&gt;CurrentHours&lt;/th&gt;
    &lt;th&gt;Scheduled PM&lt;/th&gt;
    &lt;/tr&gt;
&lt;?php while($row = $stmt-&gt;fetch()): ?&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;?php echo htmlspecialchars($row['equipType']); ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo htmlspecialchars($row['unitNumber']); ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo htmlspecialchars($row['lastUpdate']); ?&gt;&lt;/td&gt;
&lt;?php if ($row['nextPM'] - $row['currentHours'] &gt; 100): ?&gt;
    &lt;td class='good'&gt;&lt;?php echo htmlspecialchars($row['currentHours']); ?&gt;&lt;/td&gt;
    &lt;td class='good'&gt;&lt;?php echo htmlspecialchars($row['nextPM']); ?&gt;&lt;/td&gt;
&lt;?php elseif ($row['nextPM'] - $row['currentHours'] &lt; 100 &amp;&amp; $row['nextPM'] - $row['currentHours'] &gt; 50): ?&gt;
    &lt;td class='soon'&gt;&lt;?php echo htmlspecialchars($row['currentHours']); ?&gt;&lt;/td&gt;
    &lt;td class='soon'&gt;&lt;?php echo htmlspecialchars($row['nextPM']); ?&gt;&lt;/td&gt;
&lt;?php else: ?&gt;
    &lt;td class='bad'&gt;&lt;?php echo htmlspecialchars($row['currentHours']); ?&gt;&lt;/td&gt;
    &lt;td class='bad'&gt;&lt;?php echo htmlspecialchars($row['nextPM']); ?&gt;&lt;/td&gt;
&lt;?php endif; ?&gt;
    &lt;/tr&gt;
&lt;?php endwhile; ?&gt;
&lt;/table&gt;


Again, if I were doing this I would separate that top half into another file and have that file include your "template" which would output the actual HTML. You could also use a template engine like Savant3 to make the separation a little cleaner and the templating better.

Demonstration with savant3:

Your main file (index.php perhaps?):

&lt;?php
require 'DB.php'
require_once 'Savant3.php'
$tpl = new Savant3();

try {

    $stmt = $conn-&gt;query('SELECT * FROM equipment');
    $tpl-&gt;data = $stmt; 
} 
catch(PDOException $e){
    echo'ERROR: ' . $e-&gt;getMessage();
    die();
}

$tpl-&gt;display('table.tpl.php');

?&gt;


Then in your template file (table.tpl.php in this example):

&lt;table class='tableTwo'&gt;
    &lt;tr&gt;
    &lt;th&gt;Equipment Type&lt;/th&gt;
    &lt;th&gt;Unit Number&lt;/th&gt;
    &lt;th&gt;Last Updated&lt;/th&gt;
    &lt;th&gt;CurrentHours&lt;/th&gt;
    &lt;th&gt;Scheduled PM&lt;/th&gt;
    &lt;/tr&gt;
&lt;?php foreach($this-&gt;data as $row): ?&gt;
    &lt;?php $class = "bad";
    if ($row['nextPM'] - $row['currentHours'] &gt; 100) {
        $class = "good";
    }
    elseif ($row['nextPM'] - $row['currentHours'] &lt; 100 &amp;&amp; $row['nextPM'] - $row['currentHours'] &gt; 50) {
        $class = "soon";
    }
    ?&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;?php echo htmlspecialchars($row['equipType']); ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo htmlspecialchars($row['unitNumber']); ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo htmlspecialchars($row['lastUpdate']); ?&gt;&lt;/td&gt;
    &lt;td class='&lt;?php echo $class; ?&gt;'&gt;&lt;?php echo htmlspecialchars($row['currentHours']); ?&gt;&lt;/td&gt;
    &lt;td class='&lt;?php echo $class; ?&gt;'&gt;&lt;?php echo htmlspecialchars($row['nextPM']); ?&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;?php endforeach; ?&gt;
&lt;/table&gt;


Note that I changed the while into a foreach because I personally don't like doing an assignment and comparison in the same statement. You can foreach over a PDOStatement (as far as I know) just as well as you can fetch from it.

EDIT: Also, you don't necessarily need to prepare the statment with the PDOs if there are no arguments. Of course, that's all up to preference again. If you have arguments, though, definitely use the prepared statements.