php能写汉字,但是需要设置编码,其设置方法是在PHP页面中添加“header(“Content-type:text/html; charset=utf-8”);”代码即可。
本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php 不能写汉字吗?
php能写汉字,但是需要设置编码。
中文乱码
立即学习“PHP免费学习笔记(深入)”;
就这一句就好使
header("Content-type:text/html; charset=utf-8");
登录后复制
$_GET 变量
包含中文的GET变量,如果在PHP中使用,必须要转换成GBK格式,否则无法使用
<?php header("Content-type:text/html; charset=utf-8");$dirname = $_GET['dirname'];echo $dirname.'
';//必须要转换成GBK$dirname = iconv('utf-8', 'GBK', $dirname);echo $dirname.'
';?>
登录后复制
浏览器显示效果
完整例子
以遍历目录为例
<?php header("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.'
'; } } } }}read_all($dirname);?>
登录后复制
实验效果
推荐学习:《PHP视频教程》
以上就是php 不能写汉字吗的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2099627.html