Or you can use the output buffer to hold stuff before it's sent to the browser, and then send it after you've sent the cookie headers. Although I'd say that's generally bad practice.
Well, it's not bad practice, and it's actually one of it's uses but output buffers can be used for more than that. It can be used to seperate different parts of the page, and still execute non-printing code between, if needed.
Generally you should build your application from the ground like this:
1. Work with the request.
2. Send headers, cookies and so on.
3. Collect all data to be printed from e.g. a database.
4. Process the data and put it in an array, ready to be printed.
5. Print the data in the array.
Just remember that the cookies do NOT have to be at the very very top. You just have to put it before any code which print/echo information. Printing or echoing any information will automatically force the sending of headers (where cookies are stored). Like mit said, if you really have to print or echo before sending cookies you can use so called output buffers (ob_* functions, I think), because calling echo or print with ob activated will not actually send the data immediately, but will only do it when you call ob_flush() (which you put after the cookies).