標籤: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的幾種解決方案