php 不能写汉字吗
2022-12-02 09:01:16
189
{{single.collect_count}}

php能写汉字,但是需要设置编码,其设置方法是在PHP页面中添加“header("Content-type:text/html; charset=utf-8");”代码即可。

php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑

php 不能写汉字吗?

php能写汉字,但是需要设置编码。

中文乱码

就这一句就好使

header("Content-type:text/html; charset=utf-8");
登录后复制

$_GET 变量

包含中文的GET变量,如果在PHP中使用,必须要转换成GBK格式,否则无法使用

<?phpheader("Content-type:text/html; charset=utf-8");$dirname = $_GET['dirname'];echo $dirname.'<br />';//必须要转换成GBK$dirname = iconv('utf-8', 'GBK', $dirname);echo $dirname.'<br />';?>
登录后复制

浏览器显示效果

2085faa767c5ce0c2bf5cd69f01159a.png

完整例子

以遍历目录为例

<?phpheader("Content-type:text/html; charset=utf-8");$dirname = $_GET['dirname'];$dirname = iconv('utf-8', 'GBK', $dirname);function read_all ($dir){if(!is_dir($dir)) return false;$handle = opendir($dir);if($handle){while(($fl = readdir($handle)) != false){$temp = $dir.DIRECTORY_SEPARATOR.$fl;if(is_dir($temp) && $fl!='.' && $fl != '..'){read_all($temp);}else{if($fl!='.' && $fl != '..'){$temp = iconv('GBK', 'utf-8', $temp); //再将GBK编码转换为utf-8编码echo $temp.'<br>';}}}}}read_all($dirname);?>
登录后复制

实验效果

e9eaa63c57ae941a281b3497c301477.png

推荐学习:《PHP视频教程

以上就是php 不能写汉字吗的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}