如何使用在 W3C 验证器中有效的 PHP 将 HTTP 标头设置为 UTF-8 [英] How to set HTTP header to UTF-8 using PHP which is valid in W3C validator

查看:27
本文介绍了如何使用在 W3C 验证器中有效的 PHP 将 HTTP 标头设置为 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 PHP 页面在 HTML 页面,包含以下代码.

I have several PHP pages echoing out various things into HTML pages with the following code.

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

但是,当我使用 W3C 验证器 进行验证时,它出现了:

However, when I validate using the W3C validator it comes up with:

HTTP 标头中指定的字符编码(iso-8859-1)与元素中的值(utf-8)不同.

The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8).

我对 PHP 很陌生,我想知道我是否可以并且应该更改 PHP 文件的标头以匹配 HTML 文件.

I am quite new to PHP, and I was wondering if I could and should change the header for the PHP files to match the HTML files.

推荐答案

使用 header 修改 HTTP 头:

Use header to modify the HTTP header:

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

注意在任何输出发送到客户端之前调用此函数.否则,标头也已发送,您显然不能再更改它了.您可以使用 headers_sent 进行检查.有关详细信息,请参阅 header 的手册页.

Note to call this function before any output has been sent to the client. Otherwise the header has been sent too and you obviously can’t change it any more. You can check that with headers_sent. See the manual page of header for more information.

这篇关于如何使用在 W3C 验证器中有效的 PHP 将 HTTP 标头设置为 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆