摘要
在PHP运行时配置文件给你一些想法,为什么:
在开发过程中启用E_NOTICE有一些好处。
出于调试目的:NOTICE消息将警告您代码中可能存在的错误。例如,警告使用未分配的值。查找输入错误并节省调试时间非常有用。
NOTICE消息将警告您样式不良。例如,最好将$ arr [item]编写为$ arr [‘item’],因为PHP试图将“
item”视为常量。如果不是常量,PHP会假定它是数组的字符串索引。
这是每个的更详细的说明…
1.检测打字错误
E_NOTICE错误的主要原因是错别字。
示例-notice.php
<?php$username = 'joe'; // in real life this would be from $_SESSION// and then much further down in the pre...if ($usernmae) { // typo, $usernmae expands to null echo "Logged in";}else { echo "Please log in...";}?>没有E_NOTICE的输出
Please log in...
错误!你不是那个意思!
用E_NOTICE输出
Notice: Undefined variable: usernmae in /home/user/notice.php on line 3Please log in...
在PHP中,不存在的变量将返回null而不是导致错误,并且可能导致代码的行为与预期不同,因此最好注意
E_NOTICE警告。
2.检测歧义索引
它还警告您可能会改变的数组索引,例如
示例-今天的代码看起来像这样
<?php$arr = array();$arr['username'] = 'fred';// then further downecho $arr[username];?>
没有E_NOTICE的输出
fred
示例-明天您将包括图书馆
<?php// tomorrow someone adds thisinclude_once('somelib.php');$arr = array();$arr['username'] = 'fred';// then further downecho $arr[username];?>库执行以下操作:
<?phpdefine("username", "Mary");?>新的输出
空的,因为现在它扩展为:
echo $arr["Mary"];
并没有关键
Mary在
$arr。
用E_NOTICE输出
如果只有程序员
E_NOTICE使用,PHP会显示一条错误消息:
Notice: Use of undefined constant username - assumed 'username' in /home/user/example2.php on line 8fred
3.最佳原因
如果您没有解决所有
E_NOTICE您认为不是错误的错误,则您可能会变得自满,并开始忽略消息,然后有一天发生真正的错误,您将不会注意到它。



