While writing code in a file that would comprise of PHP, HTML, CSS & JavaScript, in what order each must appear? What are the best practices for separating the presentation and the logic?

Sometimes external .js and other files are using in the link tag. Where these link tags must appear?

link|improve this question

feedback

4 Answers

up vote 2 down vote accepted

This doesn't answer the question directly but the article that Rasmus Lerdorf (creator of PHP) wrote has some nice examples to follow.

Clean and simple design. HTML should look like HTML. Keep the PHP code in the views extremely simple: function calls, simple loops and variable substitutions should be all you need

http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

link|improve this answer
@Yada: That's an interesting link. – RPK Oct 30 '09 at 8:56
feedback

All of this must be in different files except of very small portions (for example js in html). The best place for link tag is in head section of html.

link|improve this answer
No. You could have PHP, HTML, CSS, and JS all in a single file. – Andrew Medico Oct 30 '09 at 5:10
You could have changed 'must be' to something like 'a better practice'. But +1 for mentioning the separation. – rahul Oct 30 '09 at 5:16
1  
Loading the javascript in the HEAD requires the rest of the page to wait on it to load. By loading it in the bottom, the rest of the page is able to load slightly faster. However, this requires a slightly different approach with how your write your javascript code since it will be loaded after the rest of the page. – Jason Oct 30 '09 at 6:42
feedback

most if not all javascript should be external files linked from either the header (between the tags) or all the way at the bottom near the closing tag depending on how youre enhancing the page.

css should never be inline in my opinion. start with external css files linked from the header, and if you must go to the file level. ie.

<head>
<style type="text/css">
/* CSS CODE HERE */
</style>
</head>

as far as php best practices, dont do a database call in your html templates. keep the templates simple php. if's for's to echo out your database results.

link|improve this answer
feedback

Your tags should (must?) go in the head of your page.

There are a handful of exceptions, but most of the time your CSS will be in an external .css file that you'll link like the .js files you mention. The order you need to load your external scripts may depend on their content. For example, if you're using jQuery plugins, you'll need to load the jQuery library before the plugin file.

PHP and HTML will often be intertwined within a document. There are discussions on SO and elsewhere over how HTML should be displayed within PHP (e.g.

?> <!--html goes here--> <?php

or

echo '<p>This is my html</p>';

..but I've never seen a definitive answer to either method. Use whichever makes you file legible.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.