为什么要修复E_NOTICE错误? [英] Why should I fix E_NOTICE errors?

查看:193
本文介绍了为什么要修复E_NOTICE错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为开发人员,我在E_NOTICE处于打开状态的情况下工作.但是最近,有人问我为什么应该修复E_NOTICE错误.我能提出的唯一理由是纠正这些问题的最佳实践.

As a developer, I work with E_NOTICE turned on. Recently though, I was asked why E_NOTICE errors should be fixed. The only reason that I could come up with was that it is best practice to correct those problems.

还有其他人有任何理由证明纠正这些问题所花费的额外时间/费用吗?

Does anyone else have any reasons to justify the extra time/cost spent to correct these problems?

更具体地说,如果代码已经有效,为什么经理要花钱修复这些问题?

More specifically, why should a manager spend the money to have these fixed if the code already works?

推荐答案

摘要

PHP运行时配置文档让您了解原因:

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

Enabling E_NOTICE during development has some benefits.

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

For debugging purposes: NOTICE messages will warn you about possible bugs in your code. For example, use of unassigned values is warned. It is extremely useful to find typos and to save time for debugging.

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

NOTICE messages will warn you about bad style. For example, $arr[item] is better to be written as $arr['item'] since PHP tries to treat "item" as constant. If it is not a constant, PHP assumes it is a string index for the array.

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

Here's a more detailed explanation of each...

1.检测打字错误

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

The main cause of E_NOTICE errors is typos.

示例-notice.php

<?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...";
}
?>

不带E_NOTICE的输出

Please log in...

错了!你不是那个意思!

Wrong! You didn't mean that!

带有E_NOTICE的输出

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

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

In PHP, a variable that doesn't exist will return null rather than causing an error, and that could cause code to behave differently than expected, so it's best to heed E_NOTICE warnings.

2.检测模糊的数组索引

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

It also warns you about array indexes that might change on you, e.g.

示例-今天的代码如下

<?php

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

// then further down

echo $arr[username];
?>

不带E_NOTICE的输出

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");
?>

新输出

空,因为现在它扩展为:

Empty, because now it expands to:

echo $arr["Mary"];

并且$arr中没有键Mary.

带有E_NOTICE的输出

如果只有程序员打开了E_NOTICE,则PHP会显示一条错误消息:

If only the programmer had E_NOTICE on, PHP would have printed an error message:

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


3.最佳原因

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

If you don't fix all the E_NOTICE errors that you think aren't errors, you will probably grow complacent, and start ignoring the messages, and then one day when a real error happens, you won't notice it.

这篇关于为什么要修复E_NOTICE错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆