調試程序的時候總是出現錯誤提示:
Notice: Undefined offset: 1 in xxx.php on line 48
Notice: Undefined offset: 2 in xxx.php on line 48
Notice: Undefined offset: 3 in xxx.php on line 48
Notice: Undefined offset: 4 in xxx.php on line 48
這問題很常出現在數組中的,程序是能正確地運行下去,但是在屏幕上總會出現這樣的提示: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
?>
這就是問題的根本原因了。