這段時間在學習php and mysql develop中的論壇例子,在調試它提供的源代碼的時候,大量報錯,如下:
( ! ) Notice: Undefined index: 2 in C:\wamp\www\treenode_class.php on line 42 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0008 | 371752 | {main}( ) | ..\index.php:0 |
2 | 0.0064 | 489016 | display_tree( ) | ..\index.php:40 |
3 | 0.0064 | 489888 | treenode->__construct( ) | ..\output_fns.php:27 |
( ! ) Notice: Undefined index: 3 in C:\wamp\www\treenode_class.php on line 42 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0008 | 371752 | {main}( ) | ..\index.php:0 |
2 | 0.0064 | 489016 | display_tree( ) | ..\index.php:40 |
3 | 0.0064 | 489888 | treenode->__construct( ) | ..\output_fns.php:27 |
( ! ) Notice: Undefined index: 4 in C:\wamp\www\treenode_class.php on line 42 | ||||
---|---|---|---|---|
Call Stack | ||||
# | Time | Memory | Function | Location |
1 | 0.0008 | 371752 | {main}( ) | ..\index.php:0 |
2 | 0.0064 | 489016 | display_tree( ) | ..\index.php:4 |
3 | 0.0064 | 489888 | treenode->__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=""