On Monday 02 October 2006 19:32, Tony Di Croce wrote: > I am relatively new to PHP... I have about 1.5 years of light PHP work > under my belt... Over the past year or so my PHP coding style has evolved > significantly and I'm curious as to how experienced programmers write > PHP... > > Basically, here is what I have evolved to: > > 1) ALL php code is at the top of the file. > 2) ALL html code is in a here document at the bottom. > 3) php code is run before 1 character is outputed (and hence, no headers > are sent, leaving redirects open for possibilities) > 4) I almost always following my require_once directives with a > session_start() at the top of the file. > 5) Often, my forms submit to the PHP page that generated them but do so > with a hidden posted variable. If that variable is set, then I process the > form submission. > > I think the most important part of all this is #1 & #2... I think I am > using PHP a little like template engine this way... > > It seems to me that I most often see code snippets that try to intertwine > HTML and PHP, but in my experience, except for trivial examples, this > doesn't work so good... > > What do you think? Good habits to form, overall. Most examples and tutorials are trivial, which is why they do the intermingling thing. To be fair, that was one of PHP's original strengths over its competition in the early days (vis, Perl and C), because it made doing simple stuff simple. You didn't need to dynamically build the whole page if you only wanted a small bit to be dynamic. Of course, once you get into anything interesting, you frequently DO want the whole page dynamic or else templated. In my case, I tend to use php-based templates and "theme functions", a style borrowed from Drupal. To wit, I have a function like this (off the cuff, not tested): function call_template($template, $args) { extract($args); ob_start(); include($template); $output = ob_get_contents(); ob_end_clean(); return $output; } $template is then the name of a mostly-HTML .php file where I can use PHP for display (mostly just echoing), making it easy to separate logic and presentation and build the page piecemeal. It also means that I have all the power of PHP for my template "engine". Then I also have functions like theme_table($header=array(), $rows=array(), $caption='', $attrib=array()) {} or theme_unorderedlist($items=array()) {} So that I can just pass logical data structures and get back fully semantically useful HTML. (Eg, theme_table automatically puts in even/odd classes on tr tags for me.) Again, credit here goes to the Drupal CMS, which uses a much more developed and refined version of this concept. -- Larry Garfield AIM: LOLG42 larry@xxxxxxxxxxxxxxxx ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php