PHP before or embedded into HTML

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
SimonJ621
Forum Commoner
Posts: 36
Joined: Thu Jul 27, 2006 5:07 am
Location: Ohio

PHP before or embedded into HTML

Post by SimonJ621 »

Hello,

A simple design question. I'm rather new to PHP and was wondering if there was a 'best practice' for placing PHP code in a document. Is it better to start with PHP or start with HTML and then initiate php within the html?

Thanks,

Jason
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Either really.

But it is usually best to keep your code separate from the design/layout depening on the application.

You could look into templating engines
User avatar
SimonJ621
Forum Commoner
Posts: 36
Joined: Thu Jul 27, 2006 5:07 am
Location: Ohio

Post by SimonJ621 »

I'm not that familiar with templating engines other than reading a little bit on SMARTY. But if I remember correctly, the design documents are seperate from the coding documents, much like CSS? So if that's the case, putting PHP inside the <body> tag doesn't mean you're mixing coding and design aspects? Or am I just wrong :oops: ?

Thanks for the quick reply,

Jason
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I honestly don't think that you should worry about it until you know more about PHP.

Stick to making pages that work and keep reading.
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

Best practice would be to seperate your html and php as much as possible. However, your html will need some PHP like loop and conditional statements.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would also interject that learning to do it the "correct" way early on will save you from trouble getting out of a bad learned habit later.

Correct in this sense is to separate business and presentation logics as has been already said.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Daedalus- wrote:I honestly don't think that you should worry about it until you know more about PHP.

Stick to making pages that work and keep reading.
Learn properly the first time and you don't have to learn a second time.
============================================================
Today's wisdom brought to you by the nice lady at the taco truck where I got my breakfast burrito.
============================================================

Programming logic is the code that tells your apps what to do. Display logic tells your pages what to show. You can mix the in PHP, but it makes it really tough later on down the road for you (or other developers) to change your design and/or presentation logic unless there is significant documentation or very sensible code-flow.

My first few apps were like that, all mixed up like speghetti and peanut butter. Then I got ahold of templating and it got better. Then I began using Template Lite (a fork of Smarty) and my apps fly and are easy to edit/maintain.

My suggestion, to reiterate was has already been stated a few times, is to develop a good practice now so as you grow in your development expertise you don't have to relearn something else later.
User avatar
SimonJ621
Forum Commoner
Posts: 36
Joined: Thu Jul 27, 2006 5:07 am
Location: Ohio

Post by SimonJ621 »

Thank you for all the replies. The PHP community is one of the reasons I was drawn to it in the first place, and it's great to see such support. And hopefully I'll learn it well the first time. Right now I'm reading two books, Beginning PHP and MySQL: From Novice to Professional from Apress by Jason Gilmore and Essential PHP Security from O'Reilly by Chris Shiflett. Hopefully these are good sources.

Thanks again,

Jason
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Shiflett frequents these forums, and yes, the book is a good resource.
Post Reply