PHP page various encoding settings

Source: Internet
Author: User
Tags character set html page php file php source code

PHP's header () function is used to define the encoding of a PHP page (note: There is no output before, otherwise the error)

PHP page is UTF8 encoding: header ("content-type:text/html; Charset=utf-8 ");

PHP page is GBK encoding: header ("content-type:text/html; CHARSET=GBK ");

Use header or meta to achieve the difference between PHP page encoding

One, page coding

1. Use label to set page encoding

The role of this label is to declare the client's browser with what character set code to display the page, XXX can be for gb2312,gbk,utf-8 (and MySQL is different, MySQL is UTF8) and so on. Therefore, most of the pages can be used in this way to tell the browser to display the page when the code, so that will not cause coding errors generated garbled. But sometimes we will find that there is no, no matter what XXX is, the browser is always a kind of coding, which I will talk about later.

Note that it is HTML information, just a declaration, and it works to show that the server has uploaded HTML information to the browser.

2. Use header ("content-type:text/html; Charset=utf-8 "); function to set page encoding

The function header () is to send the information inside the parentheses to the HTTP header.

If the contents of the parentheses in the text, that the function and the label is basically the same, we compare the first to see the characters are similar. But the difference is that if you have this function, the browser will always use your request for the XXX code, will not be disobedient, so this function is very useful.

Why does it have to be like that? The difference between HTTPS headers and HTML information is:

The HTTPS header is a string sent by the server before the HTTP protocol is used to send HTML information to the browser.

Because META tags are HTML information, the header () is sent to the browser first, and the popular point is that header () has precedence over meta. Join a PHP page with both header ("content-type:text/html; Charset=xxx "), also, the browser only to recognize the former HTTP headers and not to recognize Meta. Of course, this function can only be used within the PHP page.

There can be no output before, a space can not be otherwise error:

Warning:cannot Modify header Information-headers already sent by (output started to c:wampwwwtest.php:2) in C:wampwwwte St.php on line 3

Second, the database coding

PHP program before querying the database, first executes 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, have a piece of public code connected to the database, put in a file, in this file, add mysql_query ("set names") on it.

The set NAMES displays what character set is used in the SQL statements sent by the client. Therefore, the set NAMES ' utf-8 ' statement tells the server that "future information from this client will be based on the character set Utf-8". It also specifies the character set for the result that the server sends back to the client. (For example, if you use a SELECT statement, it represents what character set the column value uses.) )

Three, PHP page coding Unified

MySQL database code, HTML page encoding, PHP or HTML file itself to encode all the same.

1, MySQL database encoding: the establishment of the database to specify the encoding (such as GBK_CHINESE_CI), the establishment of data tables, the establishment of fields, insert data do not specify the encoding, will automatically inherit the database encoding.

When the database is connected, there is also an encoding that can be executed after the database is connected, executing mysql_query (' SET NAMES GBK '), and/or replacing GBK with your code, 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 itself encoding: with EditPlus open PHP file or HTML file, save when, select the encoding, if the database and page encoding is GBK, then here's the code select ANSI, if the database and page encoding is Utf-8, then here also choose Utf-8.

4, also note that JavaScript or flash in the data passed is UTF-8 encoding, if the database and page encoding is GBK, to do transcoding, and then write to the database. Iconv (' Utf-8 ', ' GBK ', $content);

5, in the PHP program, you can add a line to specify the PHP source code: Header (' content-type:text/html; CHARSET=GBK ');

Four, coded character case difference

(in MySQL only, other places are recommended with uppercase UTF-8, GBK, GB2312)

Only "Utf-8" Alias "UTF8" can be used in MySQL, but uppercase "UTF-8" is used everywhere else. Specifically: in the command "mysql_query (set names UTF8)" All use uppercase "UTF-8".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.