What to do if php variable outputs garbled characters

藏色散人
Release: 2023-03-09 17:44:02
Original
2118 people have browsed it

Solution to garbled output of php variables: 1. Convert through "iconv("GB2312","UTF-8",'Chinese');"; 2. Change UTF-8 to gb2312; 3 , modify the php.ini file configuration.

What to do if php variable outputs garbled characters

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

The Chinese output by echo is displayed as garbled characters,

In fact, various server scripts should encounter this problem.

It is basically an encoding problem.

Generally speaking, most pages use the page character set for encoding compatibility reasons. Defined as utf-8

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Copy after login
Copy after login

At this time, to display Chinese normally, you need to convert the encoding method, such as

echo iconv("GB2312","UTF-8",'Chinese' ; ;

Of course, the simplified Chinese page can also be simply,

Change the UTF-8 in

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Copy after login
Copy after login
to gb2312

I encountered a strange phenomenon in practice.

A page that is displayed normally on the local server will echo out garbled characters when uploaded to the server.

I haven’t thought about this reason carefully, because through iconv If the function GB2312 and UTF-8 are changed and re-encoded, it will be normal.

But it must be APACHE, or more precisely, it is caused by the different settings of the PHP server.

Look at PHP. INI should be able to solve it.

Add echo iconv("GB2312","UTF-8",'Chinese'); orIt will be more painful.

Finally, I saw that it might be a problem with the php.ini file configuration, so I opened the php.ini file and found;default.charset="UTF-8"--->default.charset="UTF-8" Remove the semicolon in front (remove the comment), restart the apache service, refresh the page, and the Chinese display will be normal.

Recommended learning: "

PHP Video Tutorial

"

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

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!