SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question html or php first in a script?

    Hi,

    I haven't been able to find much info on what the standard is for where you put html and php in a file. As in, do you need to have the php at the very top or does it not matter?

    Does having html at the top make any difference to performance? Is there times when the php should always be at the top?

    Does it slow the script down to break in out and of php for html?

    etc ..

    Be good if someone could shed some light on this topic for me. Thanks.
    coiL
    "cradled in the learning curve"

  2. #2
    SitePoint Zealot
    Join Date
    Oct 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In PHP you have to do some things *before* something is sent to the browser.

    If you want to use such functions (session_start() or header() for example) you have to put the PHP part at the top of the file.

    If you don't want to use such functions it doesn't matter what comes first.
    include_once('./sig.inc.php');

  3. #3
    SitePoint Enthusiast petersj's Avatar
    Join Date
    Aug 2002
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Always try and separate any logic in scripts from generating html unless you’re writing a function or class for specifically for the task of outputting html elements.

    For example if you write a function to get a set of results from database you could loop through your results and generate an html list. It would be better to return an array storing the data and then loop through that array later in the script.

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always put PHP first. By putting PHP first you're able to spew out error messages without messing up the HTML.

    Take this for example.

    PHP Code:
    <?php function doerror($error) {
      echo 
    "<html>".
           
    "<head></head>".
           
    "<body>$error</body>".
           
    "</html>";
      exit;
    }

    $query = @mysql_query("SELECT * FROM table");

    if (!
    $query) {
      
    doerror("Query Failed");
    }

    $result = @mysql_fetch_array($query);

    ?>

    <!-- Now your HTML page which uses the results -->
    In this example, we do all of the PHP coding before the HTML, so we can check for errors, and if there are any, an error can be echoed out. This is considerably more difficult if your PHP code is integrated into the HTML.

    PHP Code:
    <html>
      <head>
      </head>
      <body>
      Welcome to the website. Please make a selection from the list below.
        <?php $query = @mysql_query("SELECT * FROM table");
              if (!
    $query) {
                echo 
    "There was an error";
                exit;
              }
              
    $result = @mysql_fetch_array($query); ?>
      </body>
    </html>
    As you can probably see, error checking here is done in a similar way, but echoing out errors isn't quite as tidy as in the first example.

    And perhaps most importantly, in the first example, it's considerably easier to read the PHP code.
    Regards, Ant.

  5. #5
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the replies
    coiL
    "cradled in the learning curve"

  6. #6
    Non-Member
    Join Date
    Nov 2002
    Location
    Earth
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use templates

  7. #7
    pie??? PIE!!!! rsdl's Avatar
    Join Date
    May 2001
    Location
    Vancouver, BC Canada
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on your script of course, if the script requires you to have PHP first then PHP first. If it doesn't, then it doesn't really matter.

    I use a lot of includes in my scripts so i always put the includes in first before anything else.


    -rsdl


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
  •