您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为什么要修复E_NOTICE错误?

为什么要修复E_NOTICE错误?

PHP运行时配置文件给你一些想法,为什么:

在开发过程中启用E_NOTICE有一些好处。

出于调试目的:NOTICE消息将警告您代码中可能存在的错误。例如,警告使用未分配的值。查找输入错误并节省调试时间非常有用。

NOTICE消息将警告您样式不良。例如,最好将$ arr [item]编写为$ arr [‘item’],因为PHP试图将“ item”视为常量。如果不是常量,PHP会假定它是数组的字符串索引

这是每个的更详细的说明…

E_NOTICE错误的主要原因是错别字。

<?PHP
$username = 'joe';        // in real life this would be from $_SESSION

// and then much further down in the code...

if ($usernmae) {            // typo, $usernmae expands to null
    echo "Logged in";
}
else {
    echo "Please log in...";
}
?>

Please log in...

错误!你不是那个意思!

Notice: Undefined variable: usernmae in /home/user/notice.PHP on line 3
Please log in...

PHP中,不存在的变量将返回null而不是导致错误,并且可能导致代码的行为与预期不同,因此最好注意E_NOTICE警告。

它还警告您可能会改变的数组索引,例如

<?PHP

$arr = array();
$arr['username'] = 'fred';

// then further down

echo $arr[username];
?>

fred

<?PHP
// tomorrow someone adds this
include_once('somelib.PHP');

$arr = array();
$arr['username'] = 'fred';

// then further down

echo $arr[username];
?>

<?PHP
define("username", "Mary");
?>

空的,因为现在它扩展为:

echo $arr["Mary"];

并没有关键Mary$arr

如果只有程序员E_NOTICE使用,PHP显示一条错误消息:

Notice: Use of undefined constant username - assumed 'username' in /home/user/example2.PHP on line 8
fred

如果您没有解决所有E_NOTICE您认为不是错误错误,则您可能会变得自满,并开始忽略消息,然后有一天发生真正的错误,您将不会注意到它。

其他 2022/1/1 18:14:09 有600人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶