" code at the top of the PHP file; 2. Add the code in plain Set utf8 in the code of the PHP page."/> " code at the top of the PHP file; 2. Add the code in plain Set utf8 in the code of the PHP page.">

Home  >  Article  >  Backend Development  >  What to do if the Chinese characters on the php page are garbled

What to do if the Chinese characters on the php page are garbled

藏色散人
藏色散人Original
2021-03-05 17:58:328571browse

Solution to Chinese garbled characters in php page: 1. Add "

What to do if the Chinese characters on the php page are garbled

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Generally speaking, there are two reasons for the appearance of garbled characters. , one is due to the wrong encoding (charset) setting, causing the browser to parse with the wrong encoding, resulting in a messy "heavenly book" filling the screen. The second is that the file is opened with the wrong encoding and then saved, such as a The text file was originally encoded in GB2312, but if it is opened and saved in UTF-8 encoding, garbled characters will appear. This article will show you how to solve the problem of garbled characters in PHP.

We divide the garbled code situation into the following types. If necessary, you can refer to the following situations to solve the garbled code problem in a targeted manner:

The first type: Solve the problem of Chinese garbled code in HTML Method

If your HTML file is garbled, you can add UTF8 encoding (international encoding) in the head tag: UTF-8 is a country-less encoding, that is, it is independent of any Language, any language can be used.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

html5:

What to do if the Chinese characters on the php page are garbled

Second, 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"); //设置编码
?>

The third type is the Chinese garbled problem of pure PHP pages (the data is static )

If your PHP page is garbled, just add the following code at the beginning of the page.

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

The fourth type, PHP Mysql Chinese garbled problem

In addition to following the operations mentioned in the third type, 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(&#39;SET NAMES UTF8&#39;);
//接下来的就是查出数据或者修改,增加
?
如何你使用的MySQL版本在 4.1 或更高版本,可以在链接数据库操作后,设置一个字符编码,像下面这样

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, the Chinese website The two encodings mainly used in development are GB2312 and UTF-8.

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.

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of What to do if the Chinese characters on the php page are garbled. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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