Php function headers_sent not working

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

Why does headers_sent () not output in this case? Thank.

+3
source share
3 answers

Look at your php.ini configuration file and find the line containing it output_buffering, and make sure it looks like this:

output_buffering = Off

If you have Off, then echo headers_sent()should output1

If you have On, then echo headers_sent()nothing will be output, because headers_sent () in this case will return FALSE, because headers (not HTML <h1>, but HTTP response headers) are not sent yet, since the output is buffered.

To force sending headers and echoing so far, you can call flush()

+3

true false.

var_dump(headers_sent());

( )

bool(true)
bool(false)

, , , , , boolean. if, , , ternary operator

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

Edit

: 1, true, , false.

headers_sent() false? , , :

headers_sent - ,

, - , . :

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

1, headers_sent .

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

, headers_sent(). , output_buffering . output_buffering, , script , , / script.

, . , . , , .

+2

This can happen if it ob_startwas called before the code you showed was executed.

0
source

Source: https://habr.com/ru/post/1769380/


All Articles