title: Mixing php and html
body: <p>If I can ask that answers refrain from pointing towards frameworks or other libraries, I would appreciate it.  I am aware of those, and am after more of an answer based on experience for quick little hacks.</p>
  
  <p>Start with some base html</p>
  
  <pre><code> &lt;html&gt;
        &lt;head&gt;
             &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt;
             &lt;title&gt;Page Title&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
           // something is going to happen here, could be more html, or php
        &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>Let's say you will have hundreds of pages, of course, you want to modularize this in a way to have less code duplication.</p>
  
  <p>I have seen, used, and debated many a tactic.</p>
  
  <p>The basic includes method:</p>
  
  <p>header.inc.php</p>
  
  <pre><code> &lt;html&gt;
        &lt;head&gt;
             &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt;
             &lt;title&gt;Page Title&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
  </code></pre>
  
  <p>footer.inc.php</p>
  
  <pre><code>      &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>The stub of all of your hundreds of php pages:</p>
  
  <pre><code>&lt;?php include 'header.inc.php'; ?&gt;
      // html or other php can go here
  &lt;?php include 'footer.inc.php'; ?&gt;
  </code></pre>
  
  <p>Get a little more fancy, and have a var.inc.php and you could set $page_title and have that in front of the first include, saving the time of editing it out of hundreds of pages.</p>
  
  <p>The functions method:</p>
  
  <p>show.inc.php</p>
  
  <pre><code>function display_header($page_title) {
       echo "
       &lt;html&gt;
            &lt;head&gt;
                 &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt;
                 &lt;title&gt;$page_title&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;";
  
  }
  </code></pre>
  
  <p>You do the same for the footer, I suspect you get the idea.  I know a lot of you are thinking objects at this point, which I would like to also avoid for this discussion.  </p>
  
  <p>Consider you are trying to teach a very young kid some principles, or that you are trying to get a designer to move into some form of the modern age.</p>
  
  <p>Which either method, you start to run into cases where you are going to look at the output of the html and php in a browser.  There is a very clear tab indention and structure to the source output that hits the browser.  And most of the time, looking at the source of a site, I see html that is pretty abysmal.  All one line, or varying forms of indenting.</p>
  
  <p>How in a simple way do you solve this?  </p>
  
  <p>That display_header() function is going to spit in \t's all over the place, or spaces, depending on your editor, that are going to make the rendered output a mess.</p>
  
  <p>There is this option:</p>
  
  <pre><code>function display_header($page_title) {
      echo '&lt;html&gt;';
      echo '&lt;head&gt;';
      echo "\t" . '&lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt;';
      echo "\t" . '&lt;title&gt;' . $page_title . '&lt;/title&gt;';
      echo '&lt;/head&gt;';
      echo '&lt;body&gt;';
  
  }
  </code></pre>
  
  <p>Then, you get into cases of:</p>
  
  <pre><code> echo '&lt;table&gt;';
   foreach ($variable as $item) {
        echo '&lt;tr&gt;&lt;td&gt;' . $item . '&lt;/td&gt;&lt;/tr&gt;';
   }
   echo '&lt;table&gt;';
  </code></pre>
  
  <p>versus</p>
  
  <pre><code> echo "\t" . '&lt;table&gt;' . "\n";
   foreach ($variable as $item) {
        echo "\t\t". '&lt;tr&gt;&lt;td&gt;' . $item . '&lt;/td&gt;&lt;/tr&gt;' . "\n";
   }
   echo "\t" . '&lt;table&gt;' . "\n";
  </code></pre>
  
  <p>And now, you have just really made a mess of your php. So I start thinking output buffers, and I am going to totally freak the designer out with that concept.  But I have just mixed more html with php that I would be proud of.</p>
  
  <p>And I honestly have never been able to settle up with this stuff:</p>
  
  <pre><code>&lt;?php foreach ($variable as $item) { ?&gt;
   ....
  &lt;?php } ?&gt;
  </code></pre>
  
  <p>Both yields much better generated html, but to me, it also yields a more complex php file.  Pretty basic question, I am sure there will be some strong opinions, and a desire to push towards frameworks and OOP.  I understand those methods, but again, am looking for best suggestions looking at giving some tools to a person who is never going to have a passion for programming, and just wants to get stuff done, in a way that will help them be efficient.</p>