The tutorials I have been using all say that they should come at the top of the page before anything else but don’t explain what to do when they are all on the same page.
Headers, Sessions and cookies are usually dealt with first as they all output HTML headers that must be the first thing that is output to the browsers, having said that if the code above them doesnt actually cause any output (mathematical calulations, parsing of $_Post values etc) then there is no real requirement for those elements to be first.
Includes and Requires can be anywhere on the page (but of course if your including a file that issues headers then i has to obey the rules above)
as with all thses things different programmers will have different methods of coding.
You should put your require statements first. This is just good common sense to “load everything before doing anything”
If your script uses php session support, then put session_start() as early as possible,
then access your $_POST, $_GET, $_COOKIES. The order of $_GET, $_POST, $_COOKIES is not very important. You can access your $_POST or $_GET or $_COOKIES at any time in your script, even before you put anything else in the script. Think about them as variables that are available at the time of script execution. As soon as php engine starts up your php script it already has all the $_GET $_POST $_COOKIES set and available for use
If you really need to send out custom headers (most php scripts don’t really need this, only more advanced programs will send custom headers), then make sure you use header() before you use any type of echo() statement.
Headers cannot be sent after the output already started or you will get the “cannot modify header…” type of error
Edit: it’s perfectly fine to use include or require inside the conditional blocks, for example if($dothis){include (‘this.php’) } else {include (‘that.php’)}
But, it’s a good practice to require all php files that you know for sure going to be used by your program at the top of the script. This is especially true if you use APC cache. It’s been mentioned is some discussions, including comments by creator of php that APC cache really needs to know what scripts you require before it starts executing your script and any require inside the conditional blocks cannot be optimized by APC cache.