Where to put a header like PHP encoding?

I am studying UTF8 character set handling, and it is recommended that you explicitly specify the encoding type in the output headers in your PHP script as follows:

header('Content-Type: text/html; charset=utf-8');

My question is where should I place this heading. I have a configuration file that goes into every script and runs first. Should I place it at the top of this file so that this header is included first in every php file?

Will this affect my ability to set other headers, including the location of the header being redirected by line? Or should I place this immediately before any html output, for example, rendering my template file? That is, can this header be in place before any other php processing and what are the consequences of this? Does it affect the server side or just the encoding of the output?

Thank.

+5
source share
5 answers

Although early customization of this header will not affect either the server-side or other types of headers, your question is asked on the assumption that the only content that your code displays is html .

PHP , application/pdf, application/excel - .

, html .

+4

, , - (print, echo,...) header. , ( ).

+3

. php.ini, :

default_mimetype = "text/html; charset=UTF-8"

, L1. , , PHP script , .

+3

- . ; , , PHP .

, " " - - <? php? > ..

+1

-tags , (echo, print, print_r .. - HTML- -tags).

OKAY METHOD:

<?php
$var = "hello world";
$var2 = "Goodbye world!";

header();

echo $var2;
?>

OKAY METHOD:

<?php
header();
$var = "hello world";
$var2 = "Goodbye world!";



echo $var2;
?>

:

<?php
$var = "hello world";
$var2 = "Goodbye world!";

echo $var2;
header();
?>

, !

0

All Articles