title: Proper way to include code in PHP website
body: <p>I am developing a website, 150 files, lot of code there; at least for me, a begginer.</p>
  
  <p>There are many places where I repeat PHP echoes that outputs HTML, each of them with a lot of PHP variables. I am thinking about the best way not to repeat this parts.</p>
  
  <p>Given this HTML:</p>
  
  <pre><code>    &lt;main&gt;
          &lt;section&gt;
              &lt;?php
              $query = "SELECT a, b, c, d, e FROM table1";
              $result = mysqli_query($connection,$query);
              while ($row = mysqli_fetch_assoc($result)) {
                  $table1_a = $row['a'];
                  $table1_b = $row['b'];
                  $table1_c = $row['c'];
                  $table1_d = $row['d'];
                  $table1_e = $row['e'];
                  echo 'This code is thirty lines long and appears 
                  identical in many places in the website. 
                  It uses many variables, like '.$table1_a.','.$table1_b.',
                  '.$table1_c.','.$table1_d.','.$table1_e.', and others';
              }
              ?&gt;
          &lt;/section&gt;
          &lt;section&gt;
              &lt;?php
              $query = "SELECT a, b, c, d, e FROM table2";
              $result = mysqli_query($connection,$query);
              while ($row = mysqli_fetch_assoc($result)) {
                  $table2_a = $row['a'];
                  $table2_b = $row['b'];
                  $table2_c = $row['c'];
                  $table2_d = $row['d'];
                  $table2_e = $row['e'];
                  echo 'This second code is different to the other, 
                  but is thirty lines long as well and is repeated
                  in many other places of the website. 
                  It uses many variables, like'.$table2_a.',
                  '.$table2_b.','.$table2_c.','.$table2_d.',
                  '.$table2_e.' and others';
              }
              ?&gt;
          &lt;/section&gt;
      &lt;/main&gt;
  </code></pre>
  
  <p>I imagine two ways: with includes and with functions</p>
  
  <p>With the includes I would write the echoes —without the querys, because they are all different— in separated files inside the folder /includes, and then I call them in the page.</p>
  
  <p>A file in includes/echo_1.php for the includes would be like:</p>
  
  <pre><code>&lt;?php
      echo 'This second code is different to the other, 
      but is thirty lines long as well and appears identical in 
      many other places of the website. 
      It uses many variables, like '.$table2_a.','.$table2_b.','.$table2_c.',
      '.$table2_d.','.$table2_e.' and others';
  ?&gt;
  </code></pre>
  
  <p>And a function in functions/echo_1.php would be:</p>
  
  <pre><code>&lt;?php
      function echo_1($table1_a,$table1_b,$table1_c,$table1_d,$table1_e){
          echo 'This second code is different to the other, but is thirty lines
          long as well and appears identical in many other places of the website.                    
          It uses many variables, like 
          '.$table1_a.','.$table1_b.','.$table1_c.','.$table1_d.',
          '.$table1_e.' and others';
      }
  ?&gt;
  </code></pre>
  
  <p>And the calls:</p>
  
  <pre><code>    &lt;main&gt;
          &lt;section&gt;
              &lt;?php
              $query = "SELECT a, b, c, d, e FROM table1";
              $result = mysqli_query($connection,$query);
              while ($row = mysqli_fetch_assoc($result)) {
                  $table1_a = $row['a'];
                  $table1_b = $row['b'];
                  $table1_c = $row['c'];
                  $table1_d = $row['d'];
                  $table1_e = $row['e'];
                  //Calling the include from the file echo_1.php
                  include 'includes/echo_1.php';
              }
              ?&gt;
          &lt;/section&gt;
          &lt;section&gt;
              &lt;?php
              $query = "SELECT a, b, c, d, e FROM table2";
              $result = mysqli_query($connection,$query);
              while ($row = mysqli_fetch_assoc($result)) {
                  $table2_a = $row['a'];
                  $table2_b = $row['b'];
                  $table2_c = $row['c'];
                  $table2_d = $row['d'];
                  $table2_e = $row['e'];
                  //Using the function echo_1 to create the echo
                  echo_1($table1_a,$table1_b,$table1_c,$table1_d,$table1_e);
  
              }
              ?&gt;
          &lt;/section&gt;
      &lt;/main&gt;
  </code></pre>
  
  <p>Also, there is the possibility that the echoes has parts of code —with querys— that are repeated between other different echoes, and I think it would be great not to repeat them, maybe with another includes or another functions inside them.</p>
  
  <p>With includes I am going to make a lot of calls to another files, and that could slow the website. With functions I am going to make only one call, but I don't know if this would work better. Which option is the most efficient way to proceed, with includes or with functions? Any advise about writing maintainable code will be welcome!</p>
  
  <p>N.</p>