By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,746 Members | 1,865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,746 IT Pros & Developers. It's quick & easy.

"Best" coding practice?

P: n/a
Wm
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some
of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable;
?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm


Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Wm" <LA*******@hotmail.com> schreef in bericht
news:Ea**********************@news.easynews.com...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?
Personally i prefer to have one big block of php code, and echoes to print
HTML.
BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!


I seperated my database query stuff from the rest of the code by putting
that code in classes and in seperate php files. I then include_once those
files. The major reason why i decided to work like this, is that when i
decide to change some things in the structure of my database, most of my
code won't have to change. Only those classes using that particular bit of
database.

Floris

Jul 17 '05 #2

P: n/a
You should really look into templating.. There's a nice templating class
called phpLibs; I personally use the phpBB forums template class, works
incredibly well and it's very easy to use.. Keeping your code and html
separate makes it much easier to read and modify..

Regards,
Tim
"Wm" <LA*******@hotmail.com> wrote in message
news:Ea**********************@news.easynews.com...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm

Jul 17 '05 #3

P: n/a
The goal is not to seperate HTML and PHP, it's to seperate display logic
from 'buisness logic'. Using a loop inside your HTML is not a bad thing,
as long as you don't fetch your data in there.

Adding a templating engine above PHP is not always necessary. PHP can be
used as a templating engine itself if you don't use every feature of the
language, and it can be quite simple, even for a non-programmer.

timmeh wrote:
You should really look into templating.. There's a nice templating class
called phpLibs; I personally use the phpBB forums template class, works
incredibly well and it's very easy to use.. Keeping your code and html
separate makes it much easier to read and modify..

Regards,
Tim
"Wm" <LA*******@hotmail.com> wrote in message
news:Ea**********************@news.easynews.com...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see


some
of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo


$variable;
?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm



Jul 17 '05 #4

P: n/a
"Wm" <LA*******@hotmail.com> wrote in message
news:Ea**********************@news.easynews.com...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.


Hi,

take a look at TemplateTamer, it is IDE and template engine, that let's you
keep html and php in completely separate files. On related wiki you will
find several examples.

rush
--
http://www.templatetamer.com/

Jul 17 '05 #5

P: n/a
"Louis-Philippe Huberdeau" <lp*********@sympatico.ca> wrote in message
news:Vm*******************@news20.bellglobal.com.. .
The goal is not to seperate HTML and PHP, it's to seperate display logic
from 'buisness logic'. Using a loop inside your HTML is not a bad thing,
as long as you don't fetch your data in there.


Well, there are at least 2 way to look at that problem.

My prefered approach is to use template engine to separate html and php, and
MVC to separate presentation logic and bussines logic. So that are 2
separate problems, and stuffing too much into template in order to handle
whole presentation layer makes it fat (or dirty if you will), for my taste.
I prefer template to remain just an resource with markers, no form of
controls structures in it, and control structures should be in separate php
file.

But I guess it is also a matter of personal preferences, and one should use
approah that fits him the best.

rush
--
http://www.templatetamer.com/


Jul 17 '05 #6

P: n/a
I always use herdoc string output embedded into PHP, like this

<?php

.... code
.... code
.... code

print <<<BLOCKNAME

<html><head><title>
<img src="thisone">
<img src="{$fromthedatabase}">

Hello, {$name}, this is my lovely website.

BLOCKNAME;

You get all the advantages of double qoutes, but none of the hassle of
escaping quotes in HTML. Its the best form of string output when
creating large areas of HTML to be sent.

Jul 17 '05 #7

P: n/a
On Fri, 24 Oct 2003 09:30:29 +0100, Dave Bell <dr**@kent.ac.uk> wrote:
I always use herdoc string output embedded into PHP, like this


Wow - never seen that before.
Is it like printqq in Perl?
--
Games, Gizmos, Gifts and Toys from 'I want one of those'
<http://www.bizorg.co.uk/shopping/>
Jul 17 '05 #8

P: n/a
EXactly ! Thats my favourite feture too.

sanjay

"Dave Bell" <dr**@kent.ac.uk> wrote in message
news:bn**********@athena.ukc.ac.uk...
| I always use herdoc string output embedded into PHP, like this
|
| <?php
|
| ... code
| ... code
| ... code
|
| print <<<BLOCKNAME
|
| <html><head><title>
| <img src="thisone">
| <img src="{$fromthedatabase}">
|
| Hello, {$name}, this is my lovely website.
|
| BLOCKNAME;
|
| You get all the advantages of double qoutes, but none of the hassle of
| escaping quotes in HTML. Its the best form of string output when
| creating large areas of HTML to be sent.
|
Jul 17 '05 #9

P: n/a
Wm wrote:
As an amateur wannabe-pro programmer, I am trying to learn not only
how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna
see some
of my stuff!!!) I'm noticing a number of my pages have a mixture of
HTML and PHP, very interspersed. Example: A form with 20 fields where
I echo a
variable in each field to show existing data. My question is really
about the best practice to do this, as I have seen some interesting
use of includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo
$variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the
HTML at one time?

BTW, I have this same debate on a couple of pages where I do mySQL
queries 2-3 times on the same page... Advice, suggestions and
recommendations are greatly appreciated!

Thanx,
Wm

I don't mix php and html. Whenn generating an html form, I load a
single variable with all the html code. Then print $var. All the
database stuff is done with functions.

--
11:15am up 12 days, 10:31, 1 user, load average: 1.00, 1.01, 1.04
103 processes: 101 sleeping, 2 running, 0 zombie, 0 stopped
CPU states: 2.0% user, 0.7% system, 1.0% nice, 0.2% idle
To email me, change .com to .ca Linux Counter Registration #126647

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.