'PHP html DOM vs pure HTML speed

It's a simple question, but which is faster between a simple html page with php echoing results or a html page build only by php dom ?

Thank you for your answers and your advices!



Solution 1:[1]

Too many unknown variables …

When using DOM you can put much information very fast into your tree; using echo you need to track your (meta)data somehow else. To print a static page you should not use DOM. To print a page that is heavily modified during construction you would probably want to use DOM.

There is no right answer to this question. You need to do a benchmark for your specific usecase.


But you might value your time as well … I hope so. Even if it might be slower to render a page using DOM, it will be easier to let your project grow. Writing module systems that employ echo calls soon become a mess.

Solution 2:[2]

You should try some MVC Framework like Code Igniter

Wrting the same code again and again is not good.

You can split ur website like this

 ____________________________________
|                HEADER             |  // Header.php
|___________________________________| 
|                     NAV BAR       |  //menu.php
|___________________________________|
|                                   |  
|                                   |
|    Content                        | //Content {Different pages based on menu clicked}
|                                   |
|___________________________________|
|          FOOTER                   | 
|___________________________________|  //Footer.php

so All your pages will be like this

<?php
    include(header.php);
    include(menu.php);

    //based on menu clicked 
   include('clickedPage.php')

  //Finally
  include('footer.php');
?>

Solution 3:[3]

There is nothing faster than plain HTML file being accessed directly. And best served with an .html extension.

PHP code has to go through the PHP interpreter, which is an additional step.

And this is the case for every server side language (C#, PHP, Python, Java, Node.js, etc)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Naveen Kumar
Solution 3 Milan