php method to set web page encoding: 1. Add the "header("content-type:text/html;charset=utf-8");" code to the PHP file; 2. Pass "mysql_query(' SET NAMES UTF8');"Set encoding.
Recommended: "PHP Video Tutorial"
1. HTML and PHP mixed page solution
How to mix HTML and PHP, in addition to following the operations mentioned in the first method, you also need to add this code at the top of the PHP file:
<?php header("content-type:text/html;charset=utf-8"); //设置编码 ?>
2. Pure PHP page Chinese garbled code problem (the data is static)
If your PHP page has garbled characters, just add the following code to the beginning of the page.
<?php header("content-type:text/html;charset=utf-8"); //设置编码 ?>
3. PHP Mysql Chinese garbled problem
In addition to following the third operation, you must also add database encoding before your data query/modification/add. Moreover, it is worth noting that the UTF8 here is different from the previous one, there is no horizontal line in the middle.
<?php mysql_query('SET NAMES UTF8'); //接下来的就是查出数据或者修改,增加 ?
If the MySQL version you are using is 4.1 or higher, you can set a character encoding after the link database operation, like the following
UTF-8 encoding is just one of the encodings , if you don’t want to use UTF-8 encoding, you can also use other encodings. Just replace UTF-8 with the encoding you want to use. Currently, GB2312 and UTF-8 are mainly used in Chinese website development.
One thing to note is that the mysql_query("set names 'coding'"); code added before the PHP program that needs to perform database operations must be consistent with the PHP coding. If the PHP coding is gb2312, then mysql The encoding is gb2312. If it is utf-8, then the mysql encoding is utf8, so that there will be no garbled characters when inserting or retrieving data.
The above is the detailed content of How to set web page encoding in php. For more information, please follow other related articles on the PHP Chinese website!