Notice: Undefined offset 的解決辦法

來源:互聯網
上載者:User
Notice: Undefined offset 的解決方案

偵錯工具的時候總是出現錯誤提示:

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.
$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

?>

這就是問題的根本原因了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.