I am trying to better understand header()
in PHP (v7.4 if relevant) by reading the official docs and practice.
What I am struggling with is the warning that header()
should always be called before any output.
Now, I think that is easy to understand in a single file application, but what about when using require
and include
?
I don't understand, how does this work?
Asked
Active
Viewed 13 times
0

George Tiganila
- 519
- 1
- 5
- 18
-
1Did you read this yet https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – RiggsFolly Jan 29 '21 at 11:46
-
3require and include do not generate output per se, unless code within them generates output – RiggsFolly Jan 29 '21 at 11:48
-
@RiggsFolly I did and it is a bit advanced for me, the solution. Also I could not find any reference to require/include. – George Tiganila Jan 29 '21 at 12:28
-
`require` and `include` are irrelevant. They do not generate output UNLESS the code you `include` or `require` actually generates output. If they do it is exactly the same as if the main line code generated output – RiggsFolly Jan 29 '21 at 12:29
-
True, they don't. But in case code within those files do, am I forced to always call header() before them? In some cases you need to require/include from the beginning of the script... I got this much but I was hoping there is something I don't know that solves this – George Tiganila Jan 29 '21 at 12:30
-
LIke async and defer in JS with the – George Tiganila Jan 29 '21 at 12:31
-
Or is it plainly just always include/require after all calls for header() and that's that? – George Tiganila Jan 29 '21 at 12:32
-
1In reality it is rarely an issue in a well designed script. Sometimes you may need to collect the output in a variable and then echo the content of that variable a little later in the code, but its rare – RiggsFolly Jan 29 '21 at 12:32