How to solve php garbled code?

青灯夜游
Release: 2023-02-26 16:02:01
Original
5632 people have browsed it

I just learned PHP, and garbled characters often appear when writing projects. So how to solve the garbled code in php? The following article will introduce to you the solution to garbled characters in PHP. I hope it will be helpful to you.

How to solve php garbled code?

1. Chinese garbled code problem in PHP page (the data is static)

If your PHP page has garbled Chinese characters, To solve the problem, the encoding of PHP itself needs to match the encoding of the HTML web page.

Just add the following code at the beginning of the page.

<?php
header("content-type:text/html;charset=utf-8");  //设置编码
?>
Copy after login

How to solve php garbled code?

2. PHP Mysql Chinese garbled problem (data is dynamic)

In addition to following the second one In addition to operations, you must also add database coding before querying/modifying/adding your data. 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(&#39;SET NAMES UTF8&#39;);
 //接下来的就是查出数据或者修改,增加
?>
Copy after login

If you are using MySQL version 4.1 or higher, you can set a character encoding after the link database operation, like the following

How to solve php garbled code?

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 is mainly used in Chinese website development. and UTF-8 two encodings.

Note:

The mysql_query("set names 'coding'"); encoding added before the PHP program that needs to perform database operations must be consistent with the PHP encoding. , if the PHP encoding is gb2312, then the mysql 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.

For more PHP related knowledge, please visitPHP中文网!

The above is the detailed content of How to solve php garbled code?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template