FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


php/HTML pages question





DoctorBeaver
When using pages that are a mix of php & HTML, is there any definitive method of coding?

I've got a lot of pages with a mix. Which method is preferred - writing the page as php using echo "<HTMLtag>" or write the page as HTML with <?php for the php routines? Is it just a matter of which takes less work?

The main reason I'm asking is that to check some coding I've just knocked together a quick page with a form on it. The form is all there is on the page. It's in standard HTML format and I've just put <?php before it & ?> after it and it still works fine. However, when I tried that on a full page, I got an error on the <head> tag.

Does this mean that some tags are OK in php and others aren't? I've read some tutorials but they only show the 2 methods, they don't explain the whys and wherefors of each, nor whether some HTML tags are allowed and others aren't.
SirDuncan
hello,

I think you can use the method you like, I don't know a real reason for using one instead other. For example, I work with dreamweave and make the html part of a page using the dreamweaver design editor (so easy), and when I need put the php code I use the code editor in dreamweaver. So I mix html code with php code. Some times I also use the echo '<html_tag>' method, but I don't have a rule for when use which one.

DKN
DoctorBeaver
Thank you, SirDuncan. I had a feeling that was the case but I wanted to make sure.
PatTheGreat42
I usually try todo as much of the page as possible in normal HTML. Things like the <body> tag or some text at teh top I find much easier just to putin HTML.

However, if I have any text on the page that might be modified, such as a form that only shows up before the user's submitted it, I put in PHP. It just makes sense that way.
martindecorte
It could maybe be interesting, when you have a large part of pure HTML text (like lots of blabla), to put it in a .html file, and to include it from your php file.
If you apply this wisely, you'll gain modularity and evolutivity.
DoctorBeaver
Pat - that's the way I've done most of the pages. Apart from a few, there's a lot more HTML than php.


martindecorte wrote:
It could maybe be interesting, when you have a large part of pure HTML text (like lots of blabla), to put it in a .html file, and to include it from your php file.
If you apply this wisely, you'll gain modularity and evolutivity.


I've already done that with the menus and I'll probably treat the headers the same way. They're common across the whole site so it makes sense to have them as includes so I can modify them more easily.

As for actual text, there's not much commonality across pages so there's not really much point doing that.
Gieter
I mostly make an index file, containing the actual layout and the menus, which includes the page which is specified. So mostly I have more HTML in my index-page and more PHP in the other pages. I find it easy to do it that way, but of course that depends on what type of site/program you're making.
DoctorBeaver
Danke, Gieter.

Most of my pages are either mainly HTML or mainly php; there are only 1 or 2 that have about equal amounts of each. I'm using php includes for all the common coding (header, footer, ad column, usw) and all my formatting is in external CSS files. Most of my pages are just lists of includes now! Laughing
bsnyder
As most of you have said I too usually code most of the page into PHP or HTML. It makes for much easier code reading, and is much easier to maintain your code.
AftershockVibe
Remember that you don't have to keep typing echo out all the time!

Use . for concatenation (sp?) Like so

Code:

echo 'Line one<br />'
. 'Line two<br />'
. '<p>Something else....</p>';
snowboardalliance
If you are making a larger site, I recommend using a templating system like Smarty. It is the best way to separate html and php I think.
asiddle423
if you are to do a huge chunk of html i would close the php tag and write the php code in as you can see if u make any html errors and stops u from forgeting thoses .'s

if i need to put in a small php code into the script i just start and close the php tags again

and yes include files are the best way to go saves loads of work when using the same file by being able to update just one file

this is good use of oop programming which is the current way to code programs
DoctorBeaver
snowboardalliance wrote:
If you are making a larger site, I recommend using a templating system like Smarty. It is the best way to separate html and php I think.


I've already written my own CMS, it was just the refresh that was irritating me.
Kaneda
Using echo will be (a bit) slower than escaping out of PHP. That's the only real difference - other than lots of jumping in and out of PHP tends to make things quite unreadable. I usually write pure HTML pages, and then for the dynamic bits, just add a function call to an included PHP file, in order to avoid too much of a mix. So the typical header.php would be like (simplified HTML):

Code:
<?php require_once(dirname(__FILE__) . "/whatever.php"); ?>
<html>
<head>
  <title><?php insertTitle(); ?></title>
  <?php insertJS(); ?>
</head>
<body>
  <div id="header">
    <?php insertHeader(); ?>
  </div>
  <ul id="menu">
    <?php insertMenu(); ?>
  </ul>
  <div id="content">

... rather than a lot of PHP statements dropped into the HTML.

(Usually there'll be relatively more HTML, and less function calls Wink)
DoctorBeaver
Kaneda - that's more-or-less what I've done for my CMS. All the processing for deciding what to display is contained in the functions. For instance, i call display_header($page_type) and that uses a database to determine which banner and menu to display.

The only pages I had that were roughly equal HTML and php were form pages with validation. I've now separated them so the validation is on separate pages. There's hardly any php on pages that use both.
john2885
I suggest escaping out to PHP only when necessary and writing functions in files that you can call further down in the page. It generally makes the page easier to read and reduces the amount of code you have to write. I've seen people implement Smarty, which is supposed to separate logic from content, but from what I've seen it ends up over-complicating the pages.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.