Home>Article>Backend Development> How to solve the problem of garbled characters when outputting Chinese characters in PHP

How to solve the problem of garbled characters when outputting Chinese characters in PHP

王林
王林 Original
2020-07-23 13:54:22 4298browse

The solution to the garbled Chinese characters output by php is: we only need to set the encoding in the header of the php file. The specific code is: [header("content-type:text/html;charset=utf-8 ");].

How to solve the problem of garbled characters when outputting Chinese characters in PHP

php Chinese garbled code problems are generally divided into the following situations. Let’s introduce the solutions one by one:

(recommended tutorial:php tutorial)

The first type: Chinese garbled characters appear in the HTML file

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

Second type: The page mixed with HTML and PHP appears garbled

How to mix HTML and PHP, in addition to following the operations mentioned in the first method, you also need to use PHP Add this code at the top of the file:

The third type: Chinese garbled characters appear on the pure PHP page

If your PHP page appears garbled, just add the following at the beginning of the page The code will do.

The fourth type: Chinese garbled characters appear in PHP Mysql

In addition to following the operations mentioned in the third type, you must also add database encoding before querying/modifying/adding data. . Moreover, it is worth noting that the UTF8 here is different from the previous one, there is no horizontal line in the middle.

The above is the detailed content of How to solve the problem of garbled characters when outputting Chinese characters in PHP. 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