PHP Chinese output garbled how to solve? PHP Chinese output garbled solution introduction

Source: Internet
Author: User
When writing code in PHP, sometimes you may find that the Chinese output will be garbled, then, for this kind of PHP output garbled, how can we solve it? What we know is that PHP output garbled generally there are two cases, one is because the encoding Setup error, the other is because the file is encoded in the wrong code, and then save. In this article I will be specific to take a look at the PHP Chinese garbled how to solve.

We will be garbled into the following types of cases, there is a need to control the following several situations targeted to solve garbled problems

The first kind: solve the problem of Chinese garbled in HTML method

If your HTML file has garbled problems, then you can add UTF8 encoding (internationalized encoding) to the head tag: UTF-8 is no country code, which is independent of any language, any language can be used.

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

Example

We now have the HTML5 file, set the encoding much simpler, like this

The second, HTML and PHP hybrid page solution

If it is HTML and PHP mixed, in addition to follow the first method of the operation, you also need to add this code at the top of the PHP file:

<?phpheader ("Content-type:text/html;charset=utf-8");  Set the encoding?>

The third, pure PHP page of Chinese garbled problem (the data is static)

If your PHP page appears garbled, simply add the following code at the beginning of the page.

<?phpheader ("Content-type:text/html;charset=utf-8");  Set the encoding?>

The fourth kind, php+mysql Chinese garbled question

This, in addition to the third kind of operation, will also include the database code before your data query/modification/addition. Also, it is worth noting that the UTF8 here are not the same as before, and there is no horizontal line in the middle.

<?php mysql_query (' SET NAMES UTF8 '); The next step is to find out the data or modify it and add?>

If you are using the MySQL version in version 4.1 or later, you can set a character encoding after the link database operation, as follows

UTF-8 encoding is just one of the encodings, if you do not want to use UTF-8 encoding, you can use other encodings, just replace the UTF-8 you want to use the encoding you can, the current Chinese web site development is mainly used in the GB2312 and UTF-8 two kinds of coding.

One thing to note: Add the mysql_query ("Set Names ' code ') to the PHP program that needs to do the database operation, and the code must be consistent with the PHP code, if the PHP code is gb2312 the MySQL code is gb2312, If it's utf-8, the MySQL code is UTF8, so you don't get garbled when inserting or retrieving data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.