PHP Notice: undefined index xxx
虽然可以通过设置错误显示方式来隐藏这个提示,但是这样也有隐患,就是在服务器的日志中会记录这些提示,导致日志文件异常庞大!
下面是引用网上流行的解决方法:
首先,这个不是错误,是warning。所以如果服务器不能改,每个变量使用前应当先定义。
方法1:服务器配置修改
修改php.ini配置文件,error_reporting = E_ALL & ~E_NOTICE
方法2:对变量进行初始化,规范书写(比较烦琐,因为有大量的变量)。但还没有找到好定义方法,望大家指教
方法3:每个文件头部加上:error_reporting(0); 如果不行,只有打开php.ini,找到display_errors,设置为display_errors = Off。以后任何错误都不会提示。
方法4 :做判断:isset($_GET["page"]) if-else判断
或者加上’@'表示这行如果有错误或是警告不要輸出
如:@$page=$_GET["page"]
方法5:file1.php文件把$xx变量付一个值,用post 传递给file2.php,
如果file2.php没有$xx的定义,而直接使用$yy=$xx; 系统就会报错:”undifined variaable $xx”, 如果file2.php的文件开始用$xx=”";定义,那么file1.php的$xx值就传不过来了!
file2.php里可以这样
if(!isset($xx)) $xx=”";
但Jones认为,这些方法都不太方便。你不妨这样解决:
定义一个函数:
1 |
function _get( $str ){ |
2 |
$val = ! empty ( $_GET [ $str ]) ? $_GET [ $str ] : null; |
3 |
return $val ; |
4 |
} |
然后在用的时候,直接用 _get(’str’) 代替 $_GET['str'] 就行啦~
http://duanchunlingshu.blog.163.com/blog/static/1050941652010718111751666/(原始出处)
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; error_reporting is a bit-field. Or each number up to get desired error
; reporting level
; E_ALL - All errors and warnings (doesn't include E_STRICT)
所有错误和警告(除开E_STRICT)
; E_ERROR - fatal run-time errors (在运行中致命的错误)
; E_RECOVERABLE_ERROR - almost fatal run-time errors(运行中几乎致命的错误)
; E_WARNING - run-time warnings (non-fatal errors)运行中出现的警告(非致命错误)
; E_PARSE - compile-time parse errors 编译时语法错误
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string) 运行时通知(这些警告,这通常会结果
在你;从一个错误代码,但它是可能的,它是
;故意(例如使用未初始化的变量和
依托这一事实,它会被自动初始化为一个
空字符串);
; E_STRICT - run-time notices, enable to have PHP suggest changes; to your code which will ensure the best interoperability 运行时的告示,使有PHP建议更改你的代码将确保最佳的互操作性和论述了前向兼容与你的代码
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup 致命错误,发生在PHP的初始启动
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup 警告(非致命性错误),发生在PHP的
初始启动;
; E_COMPILE_ERROR - fatal compile-time errors 编译时致命的错误
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors) 编译时警告(非致命错误)
; E_USER_ERROR - user-generated error message 自创内容错误信息
; E_USER_WARNING - user-generated warning message 自创内容警告信息
; E_USER_NOTICE - user-generated notice message 自创内容通知消息