学习了那么久的php,今天我们就来了解关于php提示notice: undefined index解决方法,可能你还不了解什么意思,但是我相信当你看完这篇文章,你一定会有新的收获!
php提示notice: undefined index问题,undefined index:是指你的代码里存在:“变量还未定义、赋值就使用”的错误,这个不是致命错误,不会让你的php代码运行强行中止,但是有潜在的出问题的危险…
在读数据时出现:
notice: undefined index: name in …
notice: undefined index: key in…
源码如下:
name = isset ( name = isset(name=isset(_post[‘name’]) ? filter_input(input_post, ‘name’, filter_sanitize_special_chars) :htmlspecialchars($_get[‘name’]);key = isset ( key = isset(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的显示,屏蔽掉此类警告好,不过,建议还是改代码更好一点,代码总是写的规范一点好,将来可以少出问题啊。
问题原因:是因为你只检查p o s t 是 否 存 在 , 却 没 检 查 _post是否存在,却没检查post是否存在,却没检查_get的存在 。
完美的解决方法:修改为下面所示即可:
n a m e = i s s e t ( name = isset(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) : ‘’;k e y = i s s e t ( key = isset(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="";
推荐学习:《php视频教程》
以上就是一起了解undefined index得解决方法(精选)的详细内容。