PHP implements page encoding declaration through header and meta. I. usage:
header(“content-type:text/html; charset=xxx”);
II. differences:
The role of the tag is to declare the client browser with what character set encoding 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. Ps: Html information is only a declaration. it indicates that the server has passed HTML information to the browser.
The header () is used to send the information in the brackets to the http header. if this function is used, the browser will always use the xxx code you requested and will never be disobedient, therefore, this function is very useful. Why? The difference between the HTTPS header and the HTML information: the https header is the string sent by the server before the HTML information is sent 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 a php page with both header ("content-type: text/html; charset = xxx") and The browser only recognizes the http header instead of the meta. Of course, this function can only be used on the php page.
III. Supplement:
Sometimes, we set Label declaration, but the page does not work. This is because the default character set is set in the Apache configuration document. 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.
IV. Summary:
Priority sorting:
Header ("content-type: text/html; charset = xxx ")
Adddefacharcharset 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.