Hello everyone! This is my first post here. I have used PHP and have a general knowledge of it. In everything I have written, I have placed almost all of my html code for tables and such inside an echo. I was wondering if there is any benefit to simply inserting my html code, then opening and closing PHP tags as i need them inside the html? I'm not sure if it would possibly make things faster since you are not passing all the html through PHP, or if it would even make a difference. Thank you in advance for any and all replies! 😃

    I try to keep my PHP and HTML code seperate. Doing this with some sort of templating system.

      Thanks for the quick response. Now that I have started looking at more sample code, it seems a lot of people do separate the HTML and PHP code. I'm just wondering if it really makes a difference, because it seems a little easier to read when you just include all of your HTML in an echo?

        It may be easier for a small website. However, a large or even medium sized website becomes a nightmare when you want to maintain it and to its all a mess.

        mixing your code and html and everything together is often called spaghetti code because its impossible to see where everything is, nevermind where it starts and finishes.

        Then say you start a new (but similar) website, copying the code over becomes a nightmare because you need to remove all the HTML and take out the code you want. if its all organised and separate it shouldn't be too hard to reuse some of the code.

          I will put the large part of php tasks, such as get from database, insert database, calculation, assign values etc. before <html></html>, and then between <html></html>, mostly I will insert the php codes into html tags like this
          <?php
          //all the possible tasks that can be done first.
          // assign the results into array or variables to display later on in the html page

          ?>
          <html>
          <body>
          <?php
          //for example i will either display the results or display a form

          if (...) // this is decided in the previous php codes blocks.
          {
          ?>
          <table>
          <tr>
          <td>
          <?php echo ($results1); ?>
          </td>
          </tr>
          </table>
          <?php
          }
          else
          {
          ?>
          <form>
          ...
          </form>

          <?php
          }
          ?>
          </body>
          </html>

          In simple words, I would put the most php programming codes in a <?php ?> block, and assign the result values to variable or array before <html></html>

          and then use the <?php echo($variable); ?> and these if, for, while loop of php codes around html tags or embedded into html tags to pass the values into <html></html> to create a web page.

          When html and php tags/codes merge together. I either put the php codes around the html tags. such as

          if (...) // this is decided in the previous php codes blocks.
          {
          ?>
          <table>
          <tr>
          <td>
          Welcome back! David.
          </td>
          </tr>
          </table>
          <?php
          }
          ?>

          Or embed php codes into html tags such as

          <table>
          <tr>
          <td>
          <?php echo ($results1); ?>
          </td>
          </tr>
          </table>

          This way, a graphical desinger open the page in dreamweaver etc. He can see the page almost as a pure html page. except some place repace the text with <?php echo(...); ?> php codes.

            Well I may not be as advanced programmer as some on this site but I would suggest considering using a template engine.

            I don't recommend smarty though because it's kind of bloatware from my point of view. I think a good template engine should only be a single page so you can make small single page scripts or very large sites with 100's of php pages but only using one file template parsing file.

            I also don't suggest any of the template engines that use php as the template format lang because this just opens up the templates to hacking issues.

            I have a heavily modified FastTemplate class that I've started using just over the past few years and it makes coding much easier for both myself and our graphics gal.

            But there are several others that are just as nice but most don't have cache like fasttemplate but the fasttemplate cache I ended up gutting the cache and doing a cache class extention which works much better.

            I'm a firm believer in seperating code and html because the added bennifit to this is you also seperate business logic from presentation logic.

            With a nice cache system you can actually make your php pages display almost as fast as a straight html page, only a few miliseconds difference which doesn't matter to a user or even on shared servers.

            Using a template engine does take a little getting used to but it really isn't that big of deal.

              Yeah as said above, Templating engine of some sorts is the way to go.

              I made my own in the end as an experiment but it ended up being pretty good and does everything i need so i use that.

                Write a Reply...