I am building a new site, HTML, PHP, MYSQL.

I am having a discussion with a coworker regarding how I should handle my PHP functions that handle my SQL.

In my HTML page I call my PHP function, which accesses my MYSQL.

Should my PHP function:
- extract the data from the MYSQL, process it and return the complete HTML code? (my preference).
- extract the data from the MYSQL, return the data to the page and let the page handle the HTML portion of it (through other PHP)? (my coworker's preference).
- or do neither and put all the PHP directly in the HTML page (no external function).

Hence should my PHP function handle just the data or take care of the presentation HTML part as well. Or should it be separated from the presentation layer?

Not sure if this is strictly a philosophical question or a technical one? Are there advantages to one or the other (speed, maintainability, ...). Or are we just arguing for the fun of it?

Thanks for any ideas,

Edited by nic3500: Added details

5 Years
Discussion Span
Last Post by Weliaz

My approach in the application I work on is that PHP generates all the HTML code. I have a class that generates all parts of a page that are not affected by business logic (similar to Pear HTML_Page class, see http://pear.php.net/package/HTML_Page2). This class uses some other classes for footer, header, menus etc. Each script then uses methods of those classes and adds parts that are generated by business logic. This way the application's pages are easily maintainable. Of course I could have achieved this using other techniques like templating system (Smarty) or include files. The way I do it works fine for me since it is giving me plenty of programmatic control. Performance might be affected though, but hasn't been an issue since the application is being accessed over LAN and with limited number of users (20 concurrent users in practice, have tested with 50 and it was performing the same). Maybe this would not be a good solution for a really heavy traffic site.


I use RainTPL templating engine in most of my projects these days as it helps separate PHP/HTML. I'm not a huge fan of MVC, but a simple templater can make life a lot easier.

I started out using HTML and transplanting <?php echo ...;?> all the way through it. Yuk! Then I generated the whole lot in PHP. This was fine until I wanted to make some simple changes to the html. Had to wade through the PHP code - "now where is the function that spat out the list item??" Imagine the confusion for collaborative coding. If you're not careful, you can end up spitting out bits of html across numerous functions that come together to give a single variable. THAT'S a headache to maintain.

If you're organized and work with OOP (broj1 mentions using classes), this could be fine.

So, using a templater (like RainTPL or the more famous Smarty) works well for me as it pretty much forces me to separate HTML/PHP. Well, pretty much anyway.


Make the function return the data, as for organizing the code.

Later on, you will notice how much it helped.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.