Felipe
Felipe

Reputation: 11887

Why does this header after an echo not generate an error?

Why does this not throw an error:

echo "texto em cirílico sem conversão";
echo "русская слова";
header('Content-type: text/html; charset=utf-8');

Never mind the actual text, but I did echo some text, then sent a new header, which is obviously another header?

What am I missing here?

PS. I'm basing my thoughts on the general error which pops up when you try to send a header(location.... type of thing, but you had some errors before, and the compiler screamed about headers having already been sent.

Upvotes: 1

Views: 460

Answers (4)

BGPHiJACK
BGPHiJACK

Reputation: 1397

You are sending output before you have called header. The solution to this is either fix output_buffering values or implement the use of ob_start(); and ob_end_flush();.

I hope this helps! :)

Upvotes: 0

a1ex07
a1ex07

Reputation: 37364

Doesn't header display a warning (which may be disabled) instead of error if headers already sent ? I'd add if (headers_sent()) {die('headers_sent);'} before calling header for testing.

Upvotes: 0

Jon
Jon

Reputation: 437386

Try adding a flush(); after the echos and see if it makes a difference.

Upvotes: 1

tobyS
tobyS

Reputation: 870

I suppose you have output buffering enabled? Check your php.ini for output_buffering. If this is switched on, all printed text will first be cached in the output buffer before being sent to STDOUT.

Upvotes: 4

Related Questions