-3

In my project iam using PHP and HTML. Iam not sure how performance would reflect if I echo HTML in PHP or when I close pHP and then write HTML code.

Below are the 2 ways I can code: echo HTML within PHP

<?php
  if(!$result) {
     $qwe = 'something';
  }
  echo '<p>'.$qwe.'</p>';
  echo '<p>lots of html tags will be echoed.'</p>
  if('something happens') {
     'do something'
  }
?>

or close PHP tags and then write HTML

<?php
  if(!$result) {
     $qwe = 'something';
  }
?>
  <p><?php echo $qwe; ?></p>
  <p>lots of html tags will be echoed.</p>
<?php
  if('something happens') {
     'do something'
  }
?>

Is there any difference between these two? Any noticeable performance differences?. My project uses a lot of PHP to manipulate data and usually with more than 20k rows from MySQL query. I want to write a code which is optimised for high performance. I have been learning on query optimisation, just wondering if the above also can be tweaked.

And is there anything else I need to consider to optimise.

Abhishek Salian
  • 928
  • 2
  • 10
  • 27
  • I'd be really surprised if there were a significant performance difference. More generally, it's usually better to wait and see what is actually slow and then try to optimize that. – FoolishSeth Apr 10 '13 at 05:31
  • See Also: http://stackoverflow.com/questions/2437144/opening-closing-tags-performance?rq=1 – FoolishSeth Apr 10 '13 at 05:33
  • Read [Is micro-optimization worth the time?](http://stackoverflow.com/q/3470990/1409082) to understand why you are wasting your time with this "problem". – Jocelyn Apr 10 '13 at 10:16

3 Answers3

3

I would worry about many other performance improvements long before this, but I ran some tests and it seems that method number 1 (echo) is faster, which is actually the opposite of whatI thought it would be, but this is a tiny test case that is not conclusive in any way.

Choose whichever you and your development team works better for you for code reading/writing purposes and not for performance purposes.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

In your both way it will take same time to show the html.

If you want to create a high performance site, for that you can optimise your mysql queries and also creating indexes in it.

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

Hi When you echo HTML in PHP it goes to PHP parser and then return back to the browser. After that browser render that HTML. But if you are closing PHP tag and just writing HTML then only browser will render those HTML part.

roshan lal
  • 325
  • 1
  • 8