Home  >  Article  >  Backend Development  >  How to set character encoding to utf-8 in php

How to set character encoding to utf-8 in php

黄舟
黄舟Original
2017-08-21 16:12:5122933browse

How to set the character encoding to utf-8 in php: You can use the header("Content-Type:text/html;charset=utf-8") function to set the character encoding. This function needs to be called before any actual output is sent.

How to set character encoding to utf-8 in php

How to set UTF-8 encoding in php?

In PHP, you can use header("Content-Type:text/html;charset=utf-8"); to set the character encoding to utf-8. Note: The header() function must be called before any actual output is sent.

PHP sets the encoding of the file, the statement used:

header("Content-Type:text/html;charset=指定编码");

Add the following line of code at the beginning of the code to set the character encoding to utf-8

header("Content-Type:text/html;charset=utf-8");

Description:

The header() function sends the original HTTP header to the client.

It is important to realize that the header() function must be called before any actual output is sent.

Syntax

header(string,replace,http_response_code)

How to set character encoding to utf-8 in php

Why do we need to set the character set encoding of the file?

1 (1).png

As shown below, it is because the browser cannot correctly identify the encoding of the file, resulting in abnormal display

How to set character encoding to utf-8 in php

Example demonstration, specify the encoding of the file

How to set character encoding to utf-8 in php

Save the file and view the correctly encoded content output in the browser,

How to set character encoding to utf-8 in php

For more related knowledge, please visit PHP Chinese website! !

The above is the detailed content of How to set character encoding to utf-8 in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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