473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

"Best" coding practice?

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
9 4487
"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
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
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
"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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Aaron Davies | last post by:
I'm developing a collaborative whiteboard, in which all objects (shapes, clip art icons, etc.) are synchronized between all participants in a session. It's working well, but I'm running into a...
2
by: Svein Erik Storkaas | last post by:
I am about to add security to a web project for the first time, and i'm wondering what the easiest, yet a good way to do this? The page is just for personal use, so it does not need to be "ultra"...
5
by: Achim Domma | last post by:
Hi, I have to convert a string to its "best possible" ascii representation. It's clear to me that this is not possible or sense full for all unicode characters. But for most European characters...
8
by: elias.farah | last post by:
Hello Everyone, I'm having some very weird behavior on a couple of Access forms. (Not all forms, just some of them). The forms have been working for years, under Access XP/2003 etc, and last...
9
by: =?Utf-8?B?QW1tZXI=?= | last post by:
I've read many incomplete opinions about the "Best Practice" for securely accessing SQL but what I really need to find the "Best Practice" that fits my applications needs. Currently (alpha...
2
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but...
2
Haitashi
by: Haitashi | last post by:
I have a database that currently contains different albums. The way I had the database was that there is a "description" field in which I placed all the songs. When I access my site I see in...
1
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi everyone, I'd be interested in hearing suggestions as to the "best" way to drive a Python program step by step from another application. Details: --------- I have implemented a "Robot"...
4
by: lumpybanana247 | last post by:
Now, I understand this could have several answers, but my simple question is: What is the best free C++ compiler for Windows? For a long time, I have been using MinGW (using Bloodshed DevC++) on...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.