02-05-2007, 07:24 PM
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....
$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.
02-05-2007, 07:47 PM
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.
02-05-2007, 10:02 PM
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.
02-05-2007, 10:36 PM
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...