There are two encoding format, one is the PHP file itself encoding format, such as EditPlus, such as editor when saving the file allows you to specify the file encoding format, the other is the PHP output text encoding format, this information only for the browser, the method is <?php header (' Charset:utf-8, ');? >. It is important to note that the header method must be called before any output.
Typically, PHP project development tools such as Zendstudio, phpstorm, etc. are all set up to encode the project, ensuring that all files in the project use the same format encoding. For web output, the usual way is to use <meta charset= ' Utf-8 ' > in the HTML file to illustrate the desired encoding, which the browser interprets the content.
PHP page encoding declaration with header or meta implementation of PHP page encoding Difference
PHP header to define a PHP page for UTF encoding or GBK encoding
PHP page for UTF coded header ("content-type:text/html; Charset=utf-8 ");
PHP page for GBK coded header ("content-type:text/html; charset=gb2312 ");
PHP page for Big5 coded header ("content-type:text/html; Charset=big5 ");
Usually the above code is placed on the homepage of the PHP page
the difference between PHP page encoding using header or meta First, page encoding
1. Use <meta http-equiv= "Content-type" content= "text/html; Charset=xxx "> Label Settings page encoding
The purpose of this tag is to declare the client's browser with what character set encoding to display the page, XXX can be gb2312,gbk,utf-8 (unlike MySQL, MySQL is UTF8) and so on. As a result, most pages can use this method to tell the browser what code to use when displaying this page, so that it does not cause coding errors and garbled characters. But sometimes we will find that with this sentence or not, regardless of xxx is the kind of, the browser is always a kind of coding, this situation I will talk about later.
Note that,<meta> is an HTML message and is simply a declaration that the server has uploaded HTML information to the browser.
2. Header ("content-type:text/html; Charset=xxx ");
The function of the header () is to send the information inside the parentheses to the HTTP header.
If the contents of the parentheses are the same as in the text, the function and the <META> tag are basically the same, and the characters are similar to the one seen in the first. But the difference is that if you have this function, the browser will always take the XXX code you require, absolutely will not be disobedient, so this function is very useful. Why is that? Let's talk about the difference between the HTTPS header and the HTML message:
The HTTPS header is a string that the server sends HTML messages to the browser before the HTTP protocol.
Because the META tag is HTML information, the header () sends the content first to the browser, the popular point is the header () priority is higher than meta (I do not know if this can be said). Add a PHP page to both headers ("content-type:text/html; Charset=xxx "), but also <meta http-equiv=" Content-type "content=" text/html; Charset=xxx, the browser will only recognize the HTTP header of the former and not recognize Meta. Of course, this function can only be used within PHP pages.
There is also the question of why the former is absolutely working, and the latter sometimes not. This is the reason for the next Apache to talk about.
3. Adddefaultcharset
Apache root directory in the Conf folder, there is the entire Apache configuration document HTTPD.CONF.
With a text editor open httpd.conf, line No. 708 (different versions may be different) has adddefaultcharset xxx,xxx as the encoded name. This line of code means: Set the entire Server Web page file in the HTTPS header of the character set for your default XXX character set. With this line, it is equivalent to adding a row header to each file ("content-type:text/html; Charset=xxx "). This will understand why the obvious meta set is utf-8, can browser always use gb2312 reason.
If there is a header in the page ("content-type:text/html; Charset=xxx "), the default character set is changed to the character set you are setting, so this function is always useful. If you put Adddefaultcharset xxx in front of a "#", comment out this sentence, and the page does not contain the header ("Content-type ..."), then this time the META tag has worked.
Summarize:
Sort of.
Header ("content-type:text/html; Charset=xxx ")
Adddefaultcharset xxx
< META http-equiv= "Content-type" content= "text/html; Charset=xxx ">
If you are a Web programmer, add a header to each of your pages ("content-type:text/html; Charset=xxx ") to ensure that it can be displayed correctly on any server and is highly portable .
As for that sentence adddefaultcharset xxx, should not comment on the beholder. Anyway, I have commented out, but I write the page also to write the header (), easy to put on the server can be displayed properly.
Second, the database code PHP program before querying the database, first execute mysql_query ("SET NAMES xxxx"); where xxxx is your page encoding (CHARSET=XXXX), if the page Charset=utf8, then Xxxx=utf8, if the page charset=gb2312, then xxxx=gb2312, almost all web programs, There is a connection to the database of common code, put in a file, in this file, add mysql_query ("set names") on it.
Set NAMES shows what character set is used in the SQL statement sent by the client. Therefore, the set NAMES ' utf-8 ' statement tells the server that "the information coming from this client will be in character set Utf-8". It also specifies a character set for the result that the server sends back to the client. (For example, if you use a SELECT statement, it indicates what character set the column values use.) )
PHP page Encoding Unified
MySQL database encoding, HTML page encoding, PHP or HTML files themselves are encoded to be identical. 1, MySQL database encoding: When the database is set up to specify the encoding (such as GBK_CHINESE_CI), set up a data table, create a field, insert data, do not specify the encoding, will automatically inherit the database encoding. When the database is connected, there is also code that can execute mysql_query (' SET NAMES GBK ') after the database is connected, and/or replace GBK with your code, such as UTF8.
2, the HTML page encoding, refers to the setting of this line: <meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK "/>
3, PHP or HTML file itself encoding: with EditPlus open PHP file or HTML file, save, select the encoding, if the database and page encoding is GBK, then the code here choose ANSI; if the database and page encoding is Utf-8, The Utf-8 is also chosen here.
4, also note that thedata passed in JavaScript or flash is UTF-8 encoded, if the database and page encoding is GBK, to Transcode, and then write to the database. Iconv (' Utf-8 ', ' GBK ', $content);
5, in the PHP program, you can add a line, to specify the code of PHP source code: Header (' content-type:text/html; CHARSET=GBK ');
http://www.codeweblog.com/php-%e9%a1%b5%e9%9d%a2%e7%bc%96%e7%a0%81%e5%a3%b0%e6%98%8e%e6%96%b9%e6%b3%95%e8%af% a6%e8%a7%a3-header%e6%88%96meta/
PHP page Encoding declaration method in detail (header or meta) (GO)