Php page encoding settings _ PHP Tutorial

Source: Internet
Author: User
Php page encoding settings. The php header () function is used to define the encoding of a php page (note: No output is available before, or an error is reported). The php page is UTF-8 encoded: header (Content-type: texthtml; the header () function of charsetUTF-8 php is used to define the encoding of a php page (note: there cannot be any output before, otherwise an error is reported)

Php page is utf8 encoded: header ("Content-type: text/html; charset = UTF-8 ");

The php page is gbk encoded: header ("Content-type: text/html; charset = GBK ");

Difference in PHP page encoding using header or meta

I. Page Code

1. use tags 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 html information is only a declaration. it indicates that the server has passed HTML information to the browser.

2. use header ("content-type: text/html; charset = UTF-8"); function to set page encoding

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 in the tag. you can see that the characters are similar to those in the first one. 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? Let's talk about the differences 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 header () first reaches the browser. the common point is that header () has a higher priority than meta. Add a php page with both header ("content-type: text/html; charset = xxx") and header. then, the browser only recognizes the http header of the former and does not recognize meta. Of course, this function can only be used on the php page.

There cannot be any output or a space. Otherwise, an error is returned:

Warning: Cannot modify header information-headers already sent by (output started at C: \ wamp \ www \ test. php: 2) in C: \ wamp \ www \ test. php on line 3

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 .)

III. 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 connecting to the database, you can execute mysql_query ('set NAMES gbk'); // replace gbk with your encoding, such as utf8.

2. html page encoding refers to the setting of this line:

 

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 ');

IV. differences between uppercase and lowercase encoding characters

(Only mysql, other places are recommended to use capital UTF-8, GBK, GB2312)

The alias "utf8" for "UTF-8" can be used only in MySQL, But uppercase "UTF-8" is used elsewhere ". Specifically: the command "mysql_query (set names utf8)" will be capitalized "UTF-8 ".

The encoding () function is used to define the encoding of a php page (note: No output is available before, otherwise an error is reported). The php page is UTF-8 encoded: header (Content-type: text/html; charsets = 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.