Cito la guida ufficiale di PHP

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
in altre parole...

nelle righe di codice che precedono l'uso della funzione header() non devi assolutamente inviare output al browser dell'utente. Ciò significa che non devi inserire codice html "in chiaro", non devi usare funzioni come print ed echo e non devi neppure mettere spazi o caratteri simili prima di <?php e dopo ?> (errore comune).