在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
您认为不是错误的错误,则您可能会变得自满,并开始忽略消息,然后有一天发生真正的错误,您将不会注意到它。