Notice: Undefined index: new in E:\WWW\app\index.php on line 64
此提醒一般是因为PHP变量没有事先声明就直接用,所以系统给出提醒,并非错误
<?php $uname = $_GET['username']; if($uname == 'zhangsan'){ echo "username exists"; } else{ echo "username ok"; } ?>
运行此脚本后会出现Notice: Undefined index: username in D:\wamp64\www\WebstromDeom20190424\phpDemo\04check_register.php on line 2的警告信息。这是PHP 的提示而非报错。PHP 本身不需要事先声明变量即可直接使用,但是对未声明变量会有提示。在网站正式开始运行时,可以把提示关了的。
关闭 PHP 提示的方法:
1、打开php.ini:
error_reporting = E_ALL 改为: error_reporting = E_ALL & ~E_NOTICE
2、只要在每个文件头上加:error_reporting(0); 加上这句代码,就可以了,此方法只适用于当前页。
<?php error_reporting(0);//不会弹出警告,只适合当前页 $uname = $_GET['username']; if($uname == 'zhangsan'){ echo "username exists"; } else{ echo "username ok"; } ?>