home » 编程开发 » PHP相关 » PHP 的Undefined index,Use of undefined constant解决方法

最近玩PHP时总是碰到类似这样的情况

Notice: Use of undefined constant step2- assumed ’step2′ in F:\Internet\htdocs\11\install.php on line 35
Notice: Undefined index: step2 in F:\Internet\htdocs\11\install.php on line 57

这些是 PHP 的提示而非报错,PHP 本身不需要事先声明变量即可直接使用,但是对未声明变量会有提示。

 

当你的网站正式投入使用时最好关闭错误报告,你可以在你的PHP程序中加上

error_reporting(0);

或者把PHP.INI中的

error_reporting = E_ALL

改为:

error_reporting = E_ALL & ~E_NOTICE   重启web服务器。

 

不过我们最好是养成良好的编程习惯,

Undefined index 是指未先声明变量,你可以用isset先判断下就可以了,

例如:

if($_POST['step2'] == ‘next’) 会提示没事先声明,

if(isset($_POST['step2']) && $_POST['step2'] == ‘next’) 这样就不会有提示了。

或者加上”@”表示这行如果有错误或是警告不要輸出

如:@if($_POST['step2'] == ‘next’)

另外$_POST[step2] ,step2 如果没了引号 就会报Use of undefined constant step2 .

 

虽然php灵活,但最好还是养成好的编写习惯。

Tags: 标签:, ,
本博客所有文章如果没加特殊说明均为原创,如需转载引用请注明出处
[重阳博客:http://www.99xunle.com/archives/790]
随机文章 相关文章

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>