Jump to content

Separating PHP from HTML


Kevin M

Recommended Posts

Just want people's opinions. I'm working on a very large PHP/MySQL project. I've never really used databases before, so I'm still learning some things. My question it though, is it better to seperate the PHP from the HTML, or have it together? So would this be better:

<?php//PHP Code here?>HTML Stuff<?php//More PHP Code here?>

or this:

<?php//PHP Code Hereecho 'HTML Suff';//More PHP Code Here?>

Or does it not really matter. Basically, would it be better to echo the HTML tags/content, or seperate the PHP into code blocks and have the HTML code display?Thanks,Kevin

Link to comment
Share on other sites

I'm betting you know my answer to all of this... separation via... *see my profile title* or perhaps Smarty.From the two samples you've given however, there's a fine line. If the HTML is generated by PHP, it must be outputted by PHP (be it a simple echo or... something else). If it's static HTML (which is available on every page that this PHP page generates) it should be placed outside of the PHP enclosures.

Link to comment
Share on other sites

The first way is going to be way, way more easy to maintain in the future. I used to write all my HTML inside giant echo statements. Let me tell you that you will come back to that code 6 months or a year later and want to change how it looks, and you're going to figure it out pretty quickly that you need a better way. That led me to develop my own template engine. If you don't want to develop your own, Smarty is probably the most popular one in use, or I can also send you mine. It has several basic features, file include, if/else blocks, loop structures for an array of values, basic string replacement, etc. I wanted to develop one to only support the features that I wanted and make the template language easy for me to use, or else I probably would have used Smarty.If you're confused about what a template is, a PHP script that uses a template would look something like this. It completely separates the PHP from the HTML, there is literally no PHP code in the HTML template, and there is no HTML code in the PHP file (with some special exceptions):

<?php//some generic form processing and logic$val1 = $_POST['val1'];if ($val1 = 1)  $val2 = "yes";else  $val2 = "no";//do whatever elseadd_user($val1);//load the template and displayrequire("template.php");$page = new Page();$page->set_variable("page_title", "The Page Title");$page->set_variable("user_name", "Steve");$page->set_template("basic_page.tpl");$page->display();?>

{*type=file name=page_header.tpl}<body><h1>{*page_title}</h1>{*type=if_start name=user_name value=}  Welcome, anonymous user{*type=if_else}  Welcome, {*name}{*type=if_end}{*type=file navmenu.tpl}</body></html>

That should give you a basic idea of what it means to use templates. Those template tags I use for my own template, I'm not sure what the Smarty tags look like.

Link to comment
Share on other sites

Ok, thanks to both of you. I've got a better idea of what template engines are now too, I'll look into them. I have one other question though:If you seperate the PHP into code blocks, is it possible to only connect to the database in one of them? Example:

<?php//Connect to Database//Grab Some info?>HTML Stuff<?php//Grab some more info from database without re-connecting?>More HTML Stuff

Or would I need to re-connect to the database for each code block?

Link to comment
Share on other sites

The database connection stays open until you close it, or the script ends. If you don't close the connection yourself, you can use the same connection for as long as the script is running. You can even have an include file that connects to the database, and any file that includes that file will be able to use the connection.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...