PHP page encoding declaration differs from PHP page encoding using header or meta.
PHP header to define a PHP page as UTF or GBK Encoding
The PHP page is UTF encoded.
Header ("Content-Type: text/html; charset = UTF-8 ″);
The PHP page is GBK encoded.
Header ("Content-Type: text/html; charset = gb2312 ″);
PHP page is big5 encoded
Header ("Content-Type: text/html; charset = big5 ″);
Generally, the above Code is placed on the homepage of the PHP page.
Difference in PHP page encoding using header or Meta
I. Page code
1. Use the <meta http-equiv = "Content-Type" content = "text/html; charset = xxx"> tag to set page encoding.
The role of this label is to declare the client browser with what character set encoding to display the page, XXX Can Be gb2312, GBK, UTF-8 (and MySQL is utf8) and so on. Therefore, most pages can use this method to tell the browser what encoding is used to display the page, so as not to cause code errors and generate garbage codes. However, sometimes we will find that this sentence still does not work. No matter which type of XXX is, the browser always uses an encoding. I will talk about this later.
Note that <meta> is HTML information and is only a declaration. It indicates that the server has transmitted HTML information to the browser.
2. header ("Content-Type: text/html; charset = xxx ");
This function header () sends the information in the brackets to the HTTP header.
If the content in the brackets is as described in the text, the function is basically the same as that of the <meta> tag. You can check the first one and find that the characters are similar. But the difference is that if there is this function, the browser will always use the xxx code you requested, and it will never be disobedient, so this function is very useful. Why? So let's talk about the difference between the HTTPS header and the HTML information:
The HTTPS header is the string sent by the server before the server sends HTML information to the browser over HTTP.
Because meta tags belong to HTML information, the content sent by the header () First arrives in the browser. The common point is that the priority of Header () is higher than that of Meta (it cannot be said without knowing it ). Add the existing header ("Content-Type: text/html; charset = xxx") to a PHP page "), and <meta http-equiv = "Content-Type" content = "text/html; charset = xxx">, the browser only recognizes the HTTP header of the former and does not recognize the meta. Of course, this function can only be used on the PHP page.
There is also a problem. Why is the former absolutely effective, while the latter sometimes does not? This is the reason for Apache.
3. adddefacharcharset
The conf folder in the Apache root directory contains the entire Apache configuration file httpd. conf.
Open httpd. conf in a text editor. Line 1 (different versions may be different) has adddefacharcharset XXX, and XXX is the encoding name. Set the character set in the HTTPS header of the webpage file on the server as your default XXX character set. This line adds a header ("Content-Type: text/html; charset = xxx") to each file "). Now we can see why the meta settings are UTF-8 and the browser can always use gb2312.
If the webpage contains a header ("Content-Type: text/html; charset = xxx"), change the default character set to the character set you set, so this function will always be useful. If you add "#" before adddefacharcharset XXX and comment out this sentence, and the page does not contain the header ("Content-Type ..."), At this time, the meta tag will take effect.
Summary:
To sort
Header ("Content-Type: text/html; charset = xxx ")
Adddefacharcharset xxx
<Meta http-equiv = "Content-Type" content = "text/html; charset = xxx">
If you are a web programmer, add a header ("Content-Type: text/html; charset = xxx") to each page to ensure that it is correctly displayed on any server, high portability.
As for adddefacharcharset XXX, if you want to comment it out, you can see it. I commented it out, but I also need to write the header () to write the page so that it can be displayed on the server.
Ii. Database code
Before the PHP program queries the database, it first executes mysql_query ("set names XXXX"). XXXX is the code of Your webpage (charset = XXXX). If charset = utf8 In the webpage, then XXXX = utf8. If charset = gb2312 In the webpage, then XXXX = gb2312. Almost all web programs have a public code to connect to the database and put it in a file, in this file, add mysql_query ("set names.
Set names displays the character sets used in the SQL statements sent by the client. Therefore, the set names 'utf-8' statement tells the server that "the information sent from this client will use the UTF-8 character set ". It also specifies the character set for the results sent from the server back to the client. (For example, if you use a SELECT statement, it indicates the character set used by the column value .)
Unified PHP page Encoding
MySQL database encoding, HTML page encoding, PHP or HTML file encoding must all be consistent.
1. MySQL database encoding: Specify the encoding (such as gbk_chinese_ci) when creating a database. do not specify the encoding when creating a data table, creating a field, or inserting data. This will automatically inherit the database encoding.
The database is also encoded when it is connected. After the database is connected, run
Mysql_query ('set names gbk'); // replace GBK with your encoding, such as utf8.
2. 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 encoding: Use editplus to open the PHP file or HTML file. If the database and page encoding are GBK, the encoding here is ANSI. If the database and page encoding are UTF-8, then UTF-8 is also used.
4. In addition, the data transmitted in Javascript or flash is UTF-8 encoded. If the database and page encoding are GBK, transcode the data and write it to the database.
Iconv ('utf-8', 'gbk', $ content );
5. In the PHP program, you can add a line to specify the encoding of the PHP source program:
Header ('content-type: text/html; charset = gbk ');