Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Where do I put it?

    I have been using some PHP for a while but have never had work experience or seen how the pro's do things, so I was wondering if you could tell me, where I would put PHP in relation to the rest of my HTML.


    Say I have this....

    <?php
    $track = $_GET['track']; /* variable passed as query string in URL-bar*/
    ?>

    Would I put it in the <head> part or at the very top before the DTD?

    If someone could point to a really good resource on PHP layout etc that would be great. I've read a few tutorials but they don't give any info on best practices for writing out the code.

    Best
    CB

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    Because PHP is executed on the server and HTML is rendered in a browser, in a sense, PHP comes before any and all HTML, no matter where the PHP code is located in a file in relation to any HTML code that is contained in same file.

    PHP outputs content to the browser. This content is either in the form of straight HTML code that is located outside of the <?php ?> tags or it is HTML code that PHP generates and outputs using echo/print/print_r... statements.

    To answer your question of where or how to organize PHP code with respect to the above, the answer is the same as with any programming language in any application, put it where it logically need to be. What you are really concerned about is the overall resultant content that is sent to the browser.

    If your web page contains a number of elements - #1, #2, #3, #4... and #2 and #4 are generated using PHP, then at the point in the source file where the code for #2 and #4 would be if this was just static HTML is where you would place the PHP code.

    If that seems to be too simplistic of an explanation, it is because PHP code should simply follow the same layout and logic progression of the page in order to produce that page.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply.
    Clears that up nicely! Do you do programming for a living?
    I'm looking to get into client-side programming but want to get some work experience to see everything in action...it makes things alot clearer.

    Best,
    Clint

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    where I would put PHP in relation to the rest of my HTML.
    I think the basic rule of thumb is to try to separate your different programming languages and markup as much as possible (just makes the code cleaner and easier to read)...Edit: This is also beneficial if you have a team of people with different coding skills working on the same project...So I would say, yes, if you can put the majority of your PHP before your HTML (before the DTD), then go for it...
    Last edited by chump2877; 02-05-2007 at 09:42 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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