PHP编程中的错误

1.Undefined index ... 问题的解决方法:

 在开头加上 
error_reporting(E_ALL & ~E_NOTICE);


2.编码/乱码问题:

php页面加上或修改:

header('content-type:text/html;charset=utf-8');
带html标签的页面加上或修改:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


3.PHP之Fatal error: Cannot redeclar

PHP常见错误处理之致命错误(二)----重复定义函数


一、错误类型:PHP致命错误
Error type: PHP Fatal error
Fatal error: Cannot redeclare (a) (previously declared in (b)) in (c) on line (d)

二、错误描述:
该错误报告表示你正企图对已经定义过的函数进行再次定义,其中
a----表示重复定义的函数名;
b----第一次定义该函数时的文件名称及行号;
c----第二次定义该函数时的文件名称;
d----第二次定义该函数时的行号。

三、原因及解决方法:
原因:你连续两次使用相同名称来定义一个函数,例如
function Function(){}

function Function(){}
结果如下
Fatal error:Cannot redeclare function()(previously declared in(path):2)in(path) on line 1
解决:
找到已经声明过的函数,看看是什么需要导致你再次定义了它。如果只是单纯的忘记之前已经定义过,那么将其中一个声明删除掉就是。当然,你的情况可能给复杂。比如你的脚本文件排列异常混乱,并且你可能使用了大量的include()之类的函数,这将导致你很难从混乱的代码当中理清思路。
不过,如果你的PHP版本比较新(PHP 5.3.8+)貌似就可以用命名空间来解决那种的确有重复定义函数必要的情况。由于这个还不是太确定,所以不在这里做过多讨论。
(2)会提示Fatal error:Cannot redeclare 函数名也就是你重复声明了这个函数可以把include或者require改为include_once或者require_once
以上来自:http://www.chinabaike.com/z/jd/2012/0428/1105996.html

4.php iconv() 编码转换出错 Detected an illegal character

数原型:string iconv ( string $in_charset , string $out_charset , string $str )
特别是第二个参数说明:
the output charset.

用iconv()转换一个输出字符编码不支持的字符时,如iconv('utf-8', 'gb2312', 'www.111cn.net'),会遇到这样的错误提示:

notice: iconv() [function.iconv]: detected an illegal character in input string ...

因为gb2312表示的是简体中文,不支持像"www.111cn.net"之类的更为复杂的汉字以及一些特殊字符,这当然会报错了,解决办法有两种:

1. 扩大输出字符编码的范围,如iconv('utf-8', 'gbk', 'www.111cn.net'),则可以正确地输出,因为gbk支持的字符范围更广;

2. 在输出的字符编码字符串后面加上"//ignore",如iconv('utf-8', 'gb2312//ignore', 'www.111cn.net'),这样做其实是忽略了不能转换的字符,避免了出错但却不能够正确地输出(即空白不、输出)。


下面来看看关于php教程 iconv() : detected an illegal character in input string处理方法

$str = iconv('utf-8', 'gbk//ignore', unescape(isset($_get['str'])? $_get['str']:''));
本地测试//ignore能忽略掉它不认识的字接着往下转,并且不报错,而//translit是截掉它不认识的字及其后面的内容,并且报错。//ignore是我需要的。
现在等待上线看结果(这样不是好的做法,继续琢磨手册,上网搜搜看),呵呵。。。

在网上找到下面这篇文章,发现mb_convert_encoding也可以,但效率比iconv差。


转换字符串编码iconv与mb_convert_encoding的区别

iconv — convert string to requested character encoding(php 4 >= 4.0.5, php 5)
mb_convert_encoding — convert character encoding(php 4 >= 4.0.6, php 5)

用法:
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
需要先启用 mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉

string iconv ( string in_charset, string out_charset, string str )
注意:
第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://translit 和 //ignore,
其中:
//translit 会自动将不能直接转化的字符变成一个或多个近似的字符,
//ignore 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
returns the converted string or false on failure.

使用:
1. 发现iconv在转换字符"-"到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个"-"都无法转换成功,无法输出。另外mb_convert_encoding没有这个bug.
2. mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多;如:$str = mb_convert_encoding($str,"euc-jp","ascii,jis,euc-jp,sjis,utf- 8");“ascii,jis,euc-jp,sjis,utf-8”的顺序不同效果也有差异
3. 一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数

from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. if it is not specified, the internal encoding will be used.

$str = mb_convert_encoding($str, "ucs-2le", "jis, eucjp-win, sjis-win");
$str = mb_convert_encoding($str, "euc-jp', "auto");


5. Notice: Undefined offset: 1 in C:\wamp\test\paqu3.php on line 51

很常出现在数组中的,程序是能正确地运行下去,但是在屏幕上总会出现这样的提示:Notice: Undefined offset: ….. 网上普遍是采用抑制其显示的方法,即更改php.ini文件中error_repoting的参数为”EALL & Notice “,这样屏幕就能正常显示了.
问题是解决了,但是总想不透offset:接下去的数字(如 Notice: Undefined offset: 4 ….)是什么意思.还有,句子里的语法明明是正确的,为什么会出现警告.冷静地思考了好几遍并尝试了每种可能,终于找到了答案.offset:接下去的数字是出错的数组下标,一般是超出了数组的取值范围,如定义了数组$A[]有10个元数,如果出现了$A[10]就会出现错误(Notice: Undefined offset: 10 ….),因为数组的下标是从0开始的,所以这个数组的下标就只能是0~9.因此在出现这类问题时,不要急于用抑制显示的方法(更简单的可以在当前文件的最前面加上一句”error_reporting(填offset:接下去的那个数字);,一定要注意你所用的数组下标,仔细思考一下,问题一定会很快得到解决的 !发也有可能是unset数组后再尝试读取其内容,php手册中有:

Just to confirm, USING UNSET CAN DESTROY AN ENTIRE ARRAY. I couldn’t find reference to this anywhere so I decided to write this.
The difference between using unset and using $myarray=array(); to unset is that obviously the array will just be overwritten and will still exist.
<?php
$myarray=array(“Hello”,”World”);
echo $myarray[0].$myarray[1];
unset($myarray);
//$myarray=array();
echo $myarray[0].$myarray[1];
echo $myarray;
?>
Output with unset is:
<?
HelloWorld
Notice: Undefined offset: 0 in C:webpagesdainsidermyarray.php on line 10
Notice: Undefined offset: 1 in C:webpagesdainsidermyarray.php on line 10
Output with $myarray=array(); is:
?>
<?
HelloWorld
Notice: Undefined offset: 0 in C:webpagesdainsidermyarray.php on line 10
Notice: Undefined offset: 1 in C:webpagesdainsidermyarray.php on line 10
Array
?>
原文地址http://hi.baidu.com/putijie/item/04190dde206bd913e1f46fa9

其实在报错的那一行前面加一个@符号,屏蔽错误就ok了。非常简单。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值