Home >Backend Development >PHP Tutorial >Is your php web page garbled_PHP tutorial
1.
When children’s shoes learning PHP are writing web pages, if they are designing to store Chinese content, a problem that will most likely occur is garbled characters. If the code is generally garbled, we can check three aspects
(1) Whether the web page encoding is correct, such as whether to add the original tag
to the headermeta charset="UTF-8">
(2) Check the default character set used when storing mysql database
(3) Check the encoding of the web page file to see if it is the corresponding Chinese encoding
------------------
Environment: win7 wamp2.2
------------------
2.
Let’s start with the second point, the encoding character set used by the mysq database
(1) Open the mysql terminal, view the current settings, and determine the scope to be modified
Show variables like 'char%';
(2) Analyze based on the results,
1. If the results you display are similar to mine, that is (only character_set_system is encoded as utf8), then please follow the steps below step by step
2. Open the my.ini file, search for the keyword 'mysqld', and after finding it, observe whether there is this line
Character_set_server = utf8
If not, you should add a sentence
below it like I did
3. Search for the keyword 'client' again and see if there is this line
default_character_set = utf8
If not, add
under [client]
4. Save, restart the mysql service, and close the mysql terminal (otherwise the client code you see will not be updated)
5. Open the terminal again and enter
againShow variables like 'char%';
If the following result appears, the mysql data is set successfully
3.
The problem of web page file encoding is the most easily overlooked. This is set when saving the file encoding format.
Solution:
1. Use notepad to open the web page file, and then go to "Format"--"Convert to UTF-8 BOM-free encoding format"
2. Just save it
Problem analysis:
1. I have used it when writing php
"font-size:18px;">'set names utf8'); ?>
But there is still a garbled code problem!
Analysis: Using the above statement, only three items are modified, and these three items are
Character_set_client
Character_set_connection
Character_set_results
And this modification is only temporary, it will be the same as before after closing the terminal
Image description:
2. Let’s analyze the third garbled code problem
(1) Use sublime text3 to edit and open the php file. You can see the encoding information in the lower left corner. You can see that the current encoding is gb2312
(2) When the web page is loaded, it is equivalent to opening a file. The format read at that time is to read the web page file according to the gb2312 encoding. When the user's browser displays it, because the web page declares The character set is utf-8, so the content of the obtained file will be interpreted according to the utf-8 character set, which will cause garbled characters, but there is no problem with the content we read from the database
Web page encoding
Original gbk encoded file
Later UTF-8 encoded files
(3) Please note that sublime text3 does not give you much information when converting the encoding. Although it shows that the conversion is successful, what? The display is still the same, or our notepad is more powerful. How to modify the previous one! After the conversion is successful
3. Why is it that after I modified it as you said, it is still garbled when displayed in the mysql terminal?
Analysis:
(1) Let’s first take a look at what character set is used by cmd under windows?
You can see that the character set of cmd is gbk, which means that any content displayed on the terminal will correspond to the gbk character set, but the Chinese setting of our database is the utf8 character set. When we want to display it, naturally The database data will be read in utf8 encoding. At that time, the encoding was utf-8. Once it reaches the terminal, it will be messed up
(2) How to check?
Just use phpmyadmin, of course you have to set the utf-8 encoding we use!
Note: For more exciting tutorials, please pay attention to Bangkejia Picture and text tutorials channel,