Jump to content

Recommended Posts

Hello,

 

I'm trying to settle into a neat way to write my PHP code so that I don't keep hopping in and out of PHP with <?php ?> tags all over the place. I don't think a full template system is really appropriate for my application, so what I've been doing is writing functions that build up uber-strings of html. For example, I might have a function called 'build_user_list_table_html(), which returns a string containing all the html code for a table of users, typically encapsulated in a unique "<div>", too. Then, when I get to my HTML section, I can just do things like <?php echo build_user_list_table_html(); ?>, which doesn't get too messy.

 

My question is, is it bad to passing around these potentially huge strings in a PHP script, or is this a common way to do things?

 

Thanks and happy 2008!

Link to comment
https://forums.phpfreaks.com/topic/83949-good-way-to-mix-php-and-html/
Share on other sites

Interesting Barand, something I've always kind of figured, but certainly never tested.

 

To the OP, I do what you are saying quite often, and I consider it an extremely valid way to present your HTML. I also find that it causes me to program in a way that steers me clear of the dreaded "Headers already sent" error. When I am absolutely committed to sending a whole page, I call the functions that do it.

 

Going back to what Barand tested, I don't use the functions in that manner typically. My functions used to send content usually echo that content from within the function, so rather than doing echo html_function();, I just say html_function();. I don't know whether or not that would satisfy an increase in speed slightly over building an entire string with the function's content, then echoing the string. I should test that some time when I'm bored enough. = )

 

The whole concept here is to try and keep unnecessary PHP out of the HTML. Your PHP should be processed up front, and then like I said, when all conditions are present to be committed to sending the page, send the page! You can't avoid the occasional loop in the middle of your HTML, but you should rarely ever need to make coding decisions while your HTML is being output.

 

Early on in my PHP travels, I grew to absolutely detest code that opens and closes PHP a zillion times. What an absolute nightmare to try and trouble shoot. Without much tutoring to go on (sites like PHP Freaks weren't around back then), I just slowly developed ways around having to do it that way. One of the solutions was to use functions to echo out large chunks of ready-to-go HTML content. I guess the concept here is, don't go crazy with it... it's just one tool, one which I consider valid if used for specific reasons that make sense to YOU. That's the bottomline, isn't it? Because at the end of the day, a small negligible reduction in performance is acceptable to me if I'm 'connected' to the flow of my code. I want to be able to trouble shoot it and modify it as easily as possible while developing. The spirit of PHP is the ease with which each coder can develop a 'style' and be flexible with the architecture. A lot of hard liners would like to see PHP become more strict with architecture, but I'll let them argue... code that works, just works. If a bottleneck develops, find the section of code responsible and fix it.

 

If you believe your application suffers from some performance issues, try echoing from within the function instead of creating a huge string and returning it.

 

PhREEEk

Thanks, PhREEEk! That's a nice reply. I don't imagine I'll be suffering performance issues in the current application I'm working on, as it's just a tiny website, but perhaps in the future, I'll consider echoing out HTML as I go. Anyway, thanks for your thoughts - much appreciated. And thanks, Barand, for the link.

I've only really been writing PHP for about a year now. In my very limited experience I found it's much easier to troubleshoot code if you keep certain parts of your html locked up in functions. Usually I will keep things like the HTML header within a function, but you can call that function with parameters like $title. Having that option will allow things to be changed on the fly to represent the current page. I'm not sure you can consider this to be a template but a template system could be built using this technique. I'd imagine this technique could cause some slow downs but, it also makes entire sites much easier to maintain.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.