tags:

views:

190

answers:

4

Im currently using PHP to fetch results from a mysql db.

Im also displaying the results by building a table and all with PHP also.

My question is, would it improve loading speed if I would just call the php variables from a HTML document (PHP + HTML).

Or maybe it doesn't matter, and I should go with the ONLY PHP solution that I already have?

Thanks

+2  A: 

Catch 22. Merging large blocks PHP and HTML code together could make it faster but severely decreases the readability and thus maintainability. Personally, I would keep them as separate as possible.

Mike B
+2  A: 

Usually template rendering is not the performance bottleneck of web applications and I would definitely prefer readability over speed here. Some common areas where small changes can improve performance significantly are:

  • frontend performance optimization
  • Database interaction (look into modelling improvements, indexes, denormalization)
  • avoidance of unnecessary repeated concatenation of strings
tosh
+1. We're talking a millisecond or two, at best. Meanwhile, you're inevitably running an unindexed query for 200 ms...
Frank Farmer
+2 Focus on caching your queries efficiently
gAMBOOKa
A: 

I always embed my PHP code within HTML. O'Reilly has a great piece on doing this, http://www.oreillynet.com/pub/a/php/2001/05/03/php%5Ffoundations.html?page=2

Mike Munroe
Are you serious? Because the code on that page is unreadable and when I say unreadable, I mean I couldn't, at a glance, tell what it does. Ok, I am spoiled by python (readability counts!), but come on!
shylent
Yes, for me, I just find it easier to see exactly what PHP is doing inline within the HTML, that is, if the PHP code does not need to be extremely lengthy with a lot of logic.
Mike Munroe
A: 

Regardless of performance (whether you embed html directly in your php or not, should not matter, much), I suggest, that you learn to use a template engine (such as Smarty) as soon as possible.

Because I've just followed a link posted in one of the answers here and it reminded me how hideous it can be when logic and presentation are entangled in one big, ugly, incomprehensible mess.

I mean, come on. Forget about echo already.

shylent
For big projects/websites I totally agree that Smarty is the way to go, but for smaller websites that don't have whole teams working on them, I think just `echo`ing everything is easier, and certainly faster. Both to write, and to parse the code.
Douwe Maan
I disagree with you most severely. Smarty's overhead in terms of lines of code is like what, five or is that six lines? And it gives you the ability to display your data in any kind of way without messing with your logic, hell, without even knowing about that logic. And you can actually show your html (template) to someone who doesn't need to know php.
shylent
It's not just the overhead, smaller websites change hands very frequently and it just adds an extra skill to look for. Also, use PHP short tags for readability.
gAMBOOKa