views:

49

answers:

3
<h1>Header</h1>
<?php
echo 'teste'; // output here
echo headers_sent(); // no output here!
?>

Why headers_sent() doesn't output in this case? Thank you.

+2  A: 

Because it returns true or false.

var_dump(headers_sent());

Should display (one or the other below)

bool(true)
bool(false)

It is working, it just will not output text, if it is false, as it is not text it is a boolean value. The general use of this function is meant for an if statement, not a displaying statement, if you want to display it simply use the ternary operator

echo (headers_sent())?'true':'false';

Edit

Thanks to Victor for correcting me: It will return 1 if true, an empty string if false.

Update

Why does headers_sent() return false? Well to clarify I will post from the manual:

headers_sent — Checks if or where headers have been sent

Basically, when you have any output, that sends the headers automatically to the browser and starts the body. For example:

<?php
echo "test";
echo headers_sent(); // should yield 1
?>

That should display 1, since we had an echo statement for the headers_sent call.

<?php
echo headers_sent(); // should yield empty string
?>

That will display an empty string, given that no output is before the headers_sent() call. The above assumes that output_buffering is turned off. As with output_buffering being on it saves all output till the script is done processing then displays that output, thus the header / body tag get sent at the end of the script.

Hopefully that clears it up. If not, see the manual link I posted above and read through the examples at the manual.

Brad F Jacobs
Converting `true` to a string should yield `1` ... absence of output indicates that the function returned `false` (which yields an empty string).
Victor Nicollet
true value is printed in this way: '1'
thomas
var_dump(headers_sent()); == 'bool(false)'
thomas
Right, as Victor Nicollet stated, false yields an empty string, so if the value is false nothing will be displayed on the screen. If you want output, see the echo method I posted above to show `true` or `false` in string form.
Brad F Jacobs
Ok, false is not showed. I want to know why this code returns false. Thank you.
thomas
A: 

This could happen if ob_start has been called before the code you showed has been executed.

Victor Nicollet
ob_start was not called... this is the entire code.
thomas
+2  A: 

Look at your php.ini config file and look for line containing output_buffering and make sure it looks like:

output_buffering = Off

If you have it Off then echo headers_sent() should output 1

If you have it On then echo headers_sent() won't output anything because headers_sent() in that case will return FALSE, because headers (meaning not HTML <h1> but HTTP response headers) were not sent yet because output is buffered.

To force sending headers and output echo-ed so far you may call flush()

Kamil Szot
output_buffering = 4096
thomas
You was right. What are the consequences with this directive off?
thomas
If it's Off and you didn't called ob_start() then first echo (or print or any other command that causes output) will start outputting response to clients browser, making you unable to use functions that change http headers like header() or setcookie() - calling those functions will result in error "headers already sent"
Kamil Szot
You was right. I've set it to Off and it's working! What are the consequences with this directive off?
thomas
The directive is generally off, so it is good practice to code for it being off. All it does is saves the output in a buffer and waits for the script to finish to display everything at once. If the script is coded right, there should be no need to worry about the output_buffering being on or off as it should work for both.
Brad F Jacobs