我已經打開所有的錯誤報告來清理一些未定義的索引,只是爲了讓我的應用程序更加整潔。我注意到一個奇怪的現象:未定義索引有多嚴格?
比方說,我有以下陣列:$a = array('test' => false, 'foo' => 'bar')
如果我做if ($a['nothere'])
,我得到正確的Undefined index: nothere
通知。
但是,如果我做if ($a['test']['nothere'])
,我不要得到通知。完全一樣。儘管nothere
絕對不是$a['test']
的索引。
現在,如果我做$a['test'] = array('baz' => 'poof')
,然後運行if ($a['test']['nothere'])
確實拋出一個通知。
未定義的索引檢查不檢查空數組中的索引嗎?這是在PHP 5.2.8上。
這很可能是因爲[]字符串訪問語法和類型的雜耍。 `$ a = array('test'=> array(),'foo'=>'bar')`會產生一個錯誤 – 2011-02-07 16:03:19
呵呵,謝謝編輯以前copyedit @Piskvor引起的奇怪結構。 :P – Andrew 2011-02-07 16:03:45