您好,欢迎访问一九零五行业门户网

php提示“Notice: Undefined index”怎么办

php提示“notice: undefined index”的解决办法:首先打开“php.ini”配置文件,并修改内容为“error_reporting = e_all & ~e_notice”;然后对变量进行初始化即可。
php提示“notice: undefined index”怎么办?
php提示notice: undefined index问题,undefined index:是指你的代码里存在:“变量还未定义、赋值就使用”的错误,这个不是致命错误,不会让你的php代码运行强行中止,但是有潜在的出问题的危险......
在读数据时出现:
notice: undefined index: name in ......notice: undefined index: key in......
源码如下:
$name = isset($_post['name']) ? filter_input(input_post, 'name', filter_sanitize_special_chars) :htmlspecialchars($_get['name']);$key = isset($_post['key']) ? filter_input(input_post,'key',filter_sanitize_special_chars) :htmlspecialchars($_get['key']);
问题分析:
undefined index:是指你的代码里存在:“变量还未定义、赋值就使用”的错误,这个不是致命错误,不会让你的php代码运行强行中止,但是有潜在的出问题的危险,因此建议修改~~~~
解决方法:
用php.ini中error_reporting = e_all & ~e_notice 可以关闭notice的显示,屏蔽掉此类警告好,不过,建议还是改代码更好一点,代码总是写的规范一点好,将来可以少出问题啊。
问题原因:是因为你只检查$_post是否存在,却没检查$_get的存在 。
完美的解决方法:修改为下面所示即可:
$name = isset($_post['name']) ? filter_input(input_post,'name',filter_sanitize_special_chars) : isset($_get['name']) ? filter_input(input_post,'name',filter_sanitize_special_chars) : '';$key = isset($_post['key']) ? filter_input(input_post,'key',filter_sanitize_special_chars) : isset($_get['key']) ? filter_input(input_post,'key',filter_sanitize_special_chars) : '';
首先,这个不是错误,是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=;
==========================
方法3:每个文件头部加上:error_reporting(0);   解决
更多相关知识,请访问!
其它类似信息

推荐信息