Results 1 to 4 of 4

Thread: PHP & HTML Structure

  1. #1
    Join Date
    Feb 2013
    Location
    California
    Posts
    53
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default PHP & HTML Structure

    Is there a preferred organizational structure for including PHP code into the HTML. For example I have several PHP things that I need to do before any HTML is needed. So, do I construct the script as follows...

    <?php
    bunch of stuff
    ?>
    <!DOCTYPE >
    <html>
    <head>
    head stuff
    </head
    <body>
    html and php stuff here
    </body>
    </html>

    Or, is this approach better?


    <!DOCTYPE >
    <html>
    <head>
    head stuff
    </head
    <body>
    <?php
    bunch of stuff
    ?>
    html and php stuff here
    </body>
    </html>

    I realize that it does not really matter, I would just like to know if there is a preference for a standard one way or the other.

    TIA...
    jdadwilson

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It depends on the exact code you're writing. But generally--

    1. Do your configuration, logic and other preparation in PHP before you output any HTML. (Your first example.)
    2. Then go through your HTML layout, and use limited PHP to add in content where it needs to go, such as <?php echo $information; ?> within the right HTML <p> element.

    As much as possible, separate your HTML layout from your PHP set up. Sometimes that can be hard if you need to use conditional content, in which can you can either try to use it minimally (for example, store a value to use to check in the IF later, rather than calculating things in the middle of the HTML), or you can look into templating, or even output buffers if you must construct a lot of HTML at the top.

    There are certain things that MUST go at the top of your script, such as cookies, so in some cases you will need to do this. In other cases, it's just a good habit to get into.

    As a simple rule: if you can put it at the top of your script, do that. If not, see how little you can keep in the HTML and move the rest to the top.

    Edit: I was hoping traq would add what he'd written (I just couldn't remember the link). I'd suggest starting there:
    Last edited by djr33; 03-20-2013 at 09:11 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    keyboard (03-21-2013)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    absolutely. I wrote a bit about this subject, if you're interested.

  5. The Following 2 Users Say Thank You to traq For This Useful Post:

    jdadwilson (03-21-2013),keyboard (03-21-2013)

  6. #4
    Join Date
    Feb 2013
    Location
    California
    Posts
    53
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default

    Thanks guys for a great response. Glad to know that I'm on the right track.

Similar Threads

  1. Table Structure with CSS
    By dcr33 in forum CSS
    Replies: 5
    Last Post: 07-13-2011, 08:21 PM
  2. Structure of RSS feeds
    By biomike in forum Looking for such a script or service
    Replies: 0
    Last Post: 01-21-2010, 04:00 PM
  3. need a help for the structure of web
    By gurmeet in forum Looking for such a script or service
    Replies: 1
    Last Post: 10-30-2009, 12:43 PM
  4. php structure (MVC?)
    By Dennis_Gull in forum PHP
    Replies: 7
    Last Post: 09-10-2008, 08:36 AM
  5. Looking for a data structure that . . .
    By Trinithis in forum Other
    Replies: 0
    Last Post: 12-31-2007, 06:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •