0

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

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

+1. We're talking a millisecond or two, at best. Meanwhile, you're inevitably running an unindexed query for 200 ms...
+2 Focus on caching your queries efficiently
0

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_foundations.html?page=2

3 Comments

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!
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.
I use always plain PHP and echo's, its far more easier to read as wordprosessor has different colors for strings than code so I see what part is string. If there multiple colors like green and red from html including php blue then it is color blast and hard to see small errors in the code.
0

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.

3 Comments

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 echoing everything is easier, and certainly faster. Both to write, and to parse the code.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.