php提示undefined index的幾種解決方案

來源:互聯網
上載者:User

標籤:name   class   iss   dex   reporting   配置   apche   user   tle   

平時用$_post[‘‘],$_get[‘‘]擷取表單中參數時會出現Notice: Undefined index: --------;

我們經常接收表單POST過來的資料時報Undefined index錯誤,如下: $act=$_POST[‘action‘];
用以上代碼總是提示
Notice: Undefined index: act in D:\test\post.php on line 20
另外,有時還會出現
Notice: Undefined variable: Submit ...... 等一些這樣的提示

出現上面這些是 PHP 的提示而非報錯,PHP 本身不需要事先聲明變數即可直接使用,但是對未聲明變數會有提示。一般作為正式的網站會把提示關掉的,甚至連錯誤資訊也被關掉。

解決方案:

方法1:伺服器配置修改
修改 php.ini 中的 error配置下錯誤顯示方式:將error_reporting = E_ALL 修改為

error_reporting = E_ALL & ~E_NOTICE

修改後重啟下APCHE伺服器,方可生效。

方法2:對變數進行初始化。

方法3:做判斷isset($_post[‘‘]),empty($_post[‘‘]) if --else

方法4:在出現notice代碼之前加上@,@表示這行有錯誤或是警告不要輸出,@$username=$_post[‘username‘];
在變數前面 加上一個 @ ,如 if (@$_GET[‘action‘]==‘save‘) { ...

方法5:最後一種很實用,是別人寫的一個函數,通過這個函數進行傳遞值。

定義一個函數:

代碼如下:
function _get($str){
$val = !empty($_GET[$str]) ? $_GET[$str] : null;
return $val;
}



然後在用的時候,直接用 _get(‘str‘) 代替 $_GET[‘str‘] 就行啦~

php提示undefined index的幾種解決方案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.