Home > Backend Development > PHP Problem > Why is the php web page garbled?

Why is the php web page garbled?

青灯夜游
Release: 2023-03-13 19:36:02
Original
6064 people have browsed it

The reasons for garbled php web pages: 1. The web page encoding (charset) is set incorrectly, causing the browser to parse it with the wrong encoding; 2. The php file is opened and saved with the wrong encoding.

Why is the php web page garbled?

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

PHP is actually written In the code, some problems often occur, such as garbled pages and other problems. Next, we will introduce in detail the causes of garbled PHP pages and related solutions.

Generally speaking, there are two reasons for the appearance of garbled characters on PHP pages:

  • Due to the wrong encoding (charset) setting, the browser The wrong encoding is used to parse, resulting in a screen full of messy "Book of Heaven"

  • The file is opened with the wrong encoding and then saved. For example, a text file was originally encoded in GB2312, but it was Open it in UTF-8 encoding and save it.

To solve the above garbled PHP page problem, you first need to know which aspects of development involve encoding:

  • Page declaration encoding: in the HTML code HEAD can be used to tell the browser what encoding the web page uses. Currently, XXX mainly uses GB2312 and UTF-8 in Chinese website development.

  • Database connection encoding: refers to which encoding is used to transmit data to the database when performing database operations. It should be noted here that it should not be confused with the encoding of the database itself. For example, the internal default of MySQL is latin1 encoding, that is to say, Mysql stores data in latin1 encoding, and data transmitted to Mysql in other encodings will be converted into latin1 encoding.

Knowing where coding is involved in WEB development, you will also know the reason for garbled PHP pages: the above coding settings are inconsistent, because most of the various codings are ASCII compatible , so English symbols will not appear, and Chinese will be out of luck.

The following are some common error situations and solutions:

1. The database uses UTF8 encoding, and the page declaration encoding is GB2312. This is the most common cause of garbled characters. At this time, if you directly SELECT data in the PHP script, the PHP page will be garbled. You need to use:

mysql_query("SET NAMES GBK"); to set the MYSQL connection encoding before querying. , ensure that the page declaration encoding is consistent with the connection encoding set here (GBK is an extension of GB2312). If the page is UTF-8 encoded, you can use:

mysql_query("SET NAMES UTF8"); Note that it is UTF8 instead of the commonly used UTF-8. If the encoding of the page declaration is consistent with the internal encoding of the database, you do not need to set the connection encoding.

Note: In fact, the data input and output of MYSQL is more complicated than what is mentioned above. There are 2 default encodings defined in the MYSQL configuration file my.ini, which are default-character-set in [client] and The default-character-set in [mysqld] is used to set the encoding used by the default client connection and the internal database. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server, which tells the MYSQL server what encoding the client data received is, instead of using the default encoding.

2. The page declaration encoding is inconsistent with the encoding of the file itself. This rarely happens because if the encoding is inconsistent, what the artist sees in the browser when creating the page will be garbled. More often than not, it is caused by fixing some minor bugs after release, opening the page in the wrong encoding and then saving it. Or you use some FTP software to directly modify files online, such as CuteFTP. Due to incorrect software encoding configuration, the wrong encoding is converted.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Why is the php web page garbled?. 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