php tutorials, php form validation
Archive Tutorials
Discussion Home
Login / Register

Board List
PHP programming
General

Links
How to save gas 25% or more
Marshall Reddick
Network Marketing Forums
Income Tax Help
Lose 10 Pounds in 3 Weeks
PHP Forum Script
Online Contest

Learn PHP in 47 hours!

Back to PHP programming

Search

Separating PHP from HTML Separating PHP from HTML

Separating PHP from HTML

by uclaboyz on Thu 03/30/2006 06:56 PM

It is a very good practice to separate your PHP code from your HTML layout.  In other words, in your web page layout file, it should only contain HTML code, and no PHP code should be in there. 

How's this possible, you may ask?  Well, I will give you the exact code that I use for my websites.  But first, let me just say that when I started programming in PHP/MySQL, I would go the lazy way of combining my logic (PHP) with layout (HTML).  There are certain advantages and disadvantages for doing this. 

The advantage of mixing logic with html is that it is simple and fast.  You can output just about any html code (tables, text, paragraphs, links, etc.) with PHP's echo function.  However, good programming practice is always separating presentation from logic.   

Let me ask you this.  If you wanted to change the way your webpage looks, to be specific, you wanted to change the layout of your website, and you have PHP code embedded in your HTML code, can you just freely use a WYSIWYG editorr to move the graphics around, merge tables, change the CSS layout freely without having to worry about messing up your PHP code? 

Now you may be able to get away with an advanced HTML editor like Macromedia Dreamweaver, but if you use Microsoft Frontpage, good luck with changing your website layout containing PHP code.  For this simple reason, it is enough to convince you that you should separate logic from presentation. 

The way you separate logic from presentation would be to introduce your own HTML element/tag, then write a PHP parser that will go through your HTML file and replace your custom tags.  Thus, you can simply use these custom tags to be inserted in your html file without havnig to worry about messing up any PHP code - because the PHP code will be all written in a PHP file! 

Sounds simple?  Stick around and I will post the code here for you to use.  Please post a reply if you haven't heard back from me as I forget things easily =).

[Reply] [Back to Top]

a reminder for ya

by morcos on Tue 02/27/2007 03:53 AM

hello i was wondering if you would post your code when you got a minute :D

[Reply] [Back to Top]

Another reminder

by aa2937 on Tue 05/15/2007 02:25 AM

Sticking around for over one year and still no code posted? But your initial message is quite close of Apr. 1st. Maybe it was a joke after all.

[Reply] [Back to Top]

what can you expect

by morcos on Tue 05/15/2007 02:27 PM

yeah it was along shot with the code request, but worth a shot. summer is finally here!

[Reply] [Back to Top]

© Copyright 2006-2007.  All Rights Reserved. Site hosted by Small Business Web Design
how to start a blog | parenting tips website | Passive Income | baby websites