关于 Notice: Undefined index: 2 in C:\wamp\www\...报错

来源:互联网 发布:unity3d 节点 编辑:程序博客网 时间:2024/05/16 20:31

这段时间在学习php and mysql develop中的论坛例子,在调试它提供的源代码的时候,大量报错,如下:

( ! ) Notice: Undefined index: 2 in C:\wamp\www\treenode_class.php on line 42Call Stack#TimeMemoryFunctionLocation10.0008371752{main}( )..\index.php:020.0064489016display_tree( )..\index.php:4030.0064489888treenode->__construct( )..\output_fns.php:27


( ! ) Notice: Undefined index: 3 in C:\wamp\www\treenode_class.php on line 42Call Stack#TimeMemoryFunctionLocation10.0008371752{main}( )..\index.php:020.0064489016display_tree( )..\index.php:4030.0064489888treenode->__construct( )..\output_fns.php:27

( ! ) Notice: Undefined index: 4 in C:\wamp\www\treenode_class.php on line 42Call Stack#TimeMemoryFunctionLocation10.0008371752{main}( )..\index.php:020.0064489016display_tree( )..\index.php:430.0064489888treenode->__construct( )..\output_fns.php:27

在网上查了下,网友提供了几种解决的方法:

首先,这个不是错误,是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=""

阅读全文
0 0