Home > headlines > Why do novices learn Chinese garbled characters when learning PHP?

Why do novices learn Chinese garbled characters when learning PHP?

小云云
Release: 2017-11-03 16:29:59
Original
1891 people have browsed it

Why do Chinese garbled characters appear?

When many novice friends learn PHP, they find that the Chinese in the program will be garbled when output, so why does this kind of garbled code occur? Generally, Generally speaking, there are two reasons for the appearance of garbled characters. One is due to the incorrect encoding (charset) setting, which causes the browser to parse with the wrong encoding, resulting in a screen full of messy "heavenly books". The second is that the file is If you open and then save the file in the wrong encoding, for example, if a text file was originally encoded in GB2312 but is opened in UTF-8 encoding and then saved, 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 one: Methods to solve the Chinese garbled code problem in HTML

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

Example

Why do novices learn Chinese garbled characters when learning PHP?

For our current HTML5 files, it is simpler to set the encoding, as follows:

Why do novices learn Chinese garbled characters when learning PHP?

Second, HTML and PHP mixed page solution

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

The third type, 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.

Fourth type, PHP+Mysql Chinese Garbled code problem

In addition to following the third operation, you must also add database encoding 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.

What if the MySQL version you are using is 4.1 or In later versions, you can set a character encoding after linking to the database operation, like the following:

Why do novices learn Chinese garbled characters when learning PHP?

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'"); 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 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.


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