Home > Article > Backend Development > How to set character set in php
In PHP, you can use the header() function to set the character set. You only need to add the "header('Content-type:text/html;charset=Character encoding');" code to the head of the PHP file. That’s it.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
If our php file is displayed in the browser There is a problem with the character encoding, such as garbled characters.
At this time, we need to use the header() function to manually set the character set. The function of this function is to set the content of the response message, and the content-type field needs to be set in the response message.
content-type is used to define the type of network file and the encoding of the web page, and determines the format and encoding in which the file recipient will read the file.
Just insert the following line of code into the PHP file.
header("Content-Type:text/html;charset=字符编码");
If you want to set utf-8 encoding and let the browser process the page according to utf-8 encoding, you can:
header("Content-Type:text/html;charset=utf-8");
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to set character set in php. For more information, please follow other related articles on the PHP Chinese website!