In-depth analysis between the header and the buffer, header Buffer Analysis
Output before test header
<?phpecho 'hello world!';header('content-type: text/html;charset=utf-8;');
When I pass the test, I will succeed without any errors or warnings. I don't know how you are? But I think most of them are okay.Cannot modify header information - headers already sent
This warning means that the header information cannot be modified and the header information has been sent. Next, let's take a look at why there are two different results?
Buffer Zone
A metaphor is like the cache when we watch a movie. It will not immediately play the video between us, but will first put some of the downloaded movies into the cache, and then play them out in the cache. This is why we write php code.
Php Cache Mechanism-output_buffering
Common in phpob
Function
Ob_start: Open the output buffer.
Ob_clean: clears the buffer.
Ob_get_contents: returned buffer content
Ob_get_clean: returns the buffer content and clears it.
<? Phpob_start (); echo 'Hello world! '; Echo ob_get_contents (); // output hello world! Hello world!
InPhp. iniModify the buffer size in the configuration file.
Usually around 233 rows. The default value is 4096, which indicates 4096 bytes, which is 4 kb.
Change 4096 to 5 and run the code again.
<?phpecho 'hello world!';header('content-type: text/html;charset=utf-8;');
If no warning or error occurs during the test, the error is:Cannot modify header information - headers already sent
Analysis Between header and Buffer
Why is there no output before we say the header?
For the header function, it is like sending the original http header to the client, which declares what the webpage we are writing is, so it is wrong if there is content before this declaration, does not comply with http rules
Let's talk about the header in php.
In php, the header does not pass through the buffer zone and is directly output to the client through the server.
Interpret the previous warning Cannot modify header information
When we write some output before the header, it will first pass through the buffer zone. Therefore, even if you write the preceding information, the final output order is first indicated by the headerecho
.
However, the output content cache cannot be placed, that is, the previous output'hello world!'
> 5 bytes. It will be output directly, that is, the first input.Output 'Hello world'
Againheader(...),
In this way, no output is allowed before the actual header is violated.
Summary
In practice, we 'd better write the header at the top of the page. Because we are not sure whether the buffer of the output content before our header can be put down. I hope this article will help you learn php.