Well, you can't output any headers, including cookies, after output was started. Period.
It's a simple technical restriction, since HTTP headers need to be sent before the HTTP body. It's not PHP that's at fault here.
You should structure your app in a way that first decides what should be sent and gathers all the necessary data, then proceeds to output the actual HTML. Mixing logic inside the main HTML body is messy however you look at it. Look into the MVC pattern for example.