是否可以隐藏/隐藏PHP通知? [英] Is it alright to suppress/hide PHP notices?

查看:87
本文介绍了是否可以隐藏/隐藏PHP通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经很长时间没有任何问题地压制通知了,但是我开始怀疑自己是否做对了.我似乎找不到任何逻辑上的理由解释为什么我不应该只是抑制它们,但是其他一些人似乎认为使用error_reporting抑制它们是一件可怕的事情,但是为什么?

I've suppressed notices for quite some time with no problems whatsoever but I am beginning to wonder if I'm doing the right thing. I can't seem to find any logical reason why I shouldn't just suppress them but some other people seem to think that suppressing them using error_reporting is a horrible thing to do, but why?

与我能找到的答案最接近的东西是在这个问题,但这离我要找的答案还很远.隐藏PHP生成的所有通知是否有某种无法预料的不利影响?例如,由于存在错误,要在POST调用中将变量包含回表单中,我可以简单地使用:

The closest thing to an answer I could find was in this question but that's still far from the answer I'm looking for. Is there some sort of unforeseen downside to hiding all the notices that PHP generates? For example, to include a variable from a POST call back into the form because there were errors, I would simply use:

<?= $_POST['variable'] ?>

这将生成一个PHP通知.要修正该通知,我可以使用类似以下的方法:

This would generate a PHP notice. To fix that notice, I could use something like this:

<?= isset($_POST['variable']) ? $_POST['variable'] : '' ?>

但是,这真的有必要吗?我的代码会真正从中受益吗,而不仅仅是回显变量是否存在并可能创建PHP通知?在我看来,能够忽略通知是使用PHP的好处,因为您不必担心是否定义了变量,尤其是对于像这样的示例而言,它似乎无关紧要.

But, is this really necessary? Will my code actually benefit any from doing this rather than just echoing the variable whether it exists or not and potentially creating a PHP notice? It seems to me that being able to ignore notices is a benefit of using PHP as then you don't have to worry about whether a variable is defined or not, especially for an example such as this where it doesn't seem to matter.

我还利用PHP的能力,根据变量的使用方式自动更改变量的类型/类型,并且您经常会发现如下代码片段:

I also take advantage of PHP's ability to automatically change a variable's type/casting depending on how it's being used and you will often find code snippets such as this:

for ($i = 0; $i < $limit; $i++) $results[] = $i; // Example

其中$results以前没有定义,但是当我尝试向其中添加新项作为数组时变成了数组.我有点喜欢这样做,因为如果没有结果添加到数组中并且我出于某种原因需要存储该信息或将其转换为JSON,则将不会定义该特定变量,从而节省了额外的带宽,即使它是分钟.

where $results has not been previously defined, but is turned into an array when I try to add a new item to it as an array. I sort of prefer doing it this way because if no results are added to the array and I need to store that information or convert it to JSON for whatever reason, then that particular variable will not be defined and thus save additional bandwidth, even if it's minute.

$data = stdClass; // For reference, in my case this would be defined before this code
$data->results = array();
$limit = 0;
for ($i = 0; $i < $limit; $i++) $data->results[] = $i;
print json_encode($data);
// {"results":[]}

$data = stdClass; // For reference
$limit = 0;
for ($i = 0; $i < $limit; $i++) $data->results[] = $i;
print json_encode($data);
// []

再次提出问题:修复通知错误而不是仅仅消除通知错误,我可以获得什么真正的好处?如何/会损害我的代码?

The question again: what real benefit, if any, do I gain from fixing notice errors rather than just suppressing them? How can/would it harm my code?

推荐答案

在我看来,您永远都不要抑制错误,无论是否出现错误.它现在可能会给您带来一些便利,但是一路走来,在维护代码时,您将面临许多很多问题.

In my point of view, you should never suppress errors, any kind of them, notices or not. It might give you some convenience right now, but down the road, you'll face many, many problems with your code when you are maintaining it.

假设您有一个想要回显的变量,就像上面的第一个示例一样.是的,使用isset有点复杂,但是也许您的应用程序仍然应该处理特殊的空情况,从而改善体验.示例:

Suppose you have a variable you want to echo out like the above first example. Yes, using isset is a little complicated, but maybe your application should handle the special empty case anyway, thus improving the experience. Example:

if (isset($var)) {
    echo $var;
} else {
    echo "Nothing is found. Try again later.";
}

如果您只有echo $var;,并且如果这是用户正在阅读的面向公众的视图,则他们在那里什么也看不到,这可能会引起混乱.当然,这只是修复PHP Notices可以改善您的应用程序的一种特殊情况.

If you only had echo $var; and if this was a public facing view a user was reading, they would just see nothing there, which may cause confusion. Of course, this is just one special case where fixing PHP Notices can improve your application.

当您处理PHP代码中的注意事项时,不应将它视为麻烦或不便之处,因为代码应该是干净的.我宁愿有一个没有通知的代码,也不希望在源代码中打开它时看到干净的代码.当然,两者绝对更好! :)

It shouldn't be taken as a trouble or inconvenience when you are taking care of notices in PHP code, because code is supposed to be clean. I'd rather have a notice-free code than seeing clean code when I open it in source. Of course, both is definitely better! :)

同样,错误(即使不是致命错误)也将在以后引起问题.如果您已经在做echo $var;之类的事情而没有对其进行检查,那就假设存在一个变量,即使您知道它可能不存在,它也会让您养成假设存在的习惯, 工作.现在这可能很小,但是过了一会儿,您会发现自己会造成很多很多问题.

Again, the errors (even if they aren't fatal) will cause problems down the road. If you're already doing things like echo $var; without checking it, that's an assumption that a variable exists, even if you know it might not, it will just give you a habit of assuming things exist and work. This might be small right now, but after a while you'll find out that you'll cause yourself many, many problems.

那里的通知是有原因的.如果我们都在代码中执行了error_reporting(E_ALL ^ E_NOTICE),则说明我们对代码不负责任.如果您能够修复它,那么为什么要懒而不做呢?当然,附带通知的1.0版会在以后修复,这就是我们所有人所说的.但是最好这样做是一种习惯,第一次编写完美代码.如果您花15分​​钟时间编写受通知困扰的代码,然后花2个小时在以后的开发时间修复中,为什么不花一小时半的时间来完善代码呢? ?

The notices are there for a reason. If we all did error_reporting(E_ALL ^ E_NOTICE) in our code, we're just being irresponsible for our code. If you are able to fix it, then why are you being lazy and not doing so? Sure, ship 1.0 with notices, fix them later, that's what we all say. But it is better to do that as a habit, code perfect the first time. If you spend 15 minutes writing code plagued by notices, and then spend 2 hours in later development time fixing them, why not just spend an hour and a half perfecting the code as you write it in the first place?

编写好的代码应该是一种习惯,而不是给您带来不便. 出现错误消息是有原因的.尊重它们,修复它们,然后您就可以成为负责任的程序员.

Writing good code should be a habit, not an inconvenience. Error messages are there for a reason. Respect them, fix them, and there you go, you're a responsible programmer.

您还为将来的代码维护者铺平了道路.

You also pave a path for the future maintainers of your code.

这篇关于是否可以隐藏/隐藏PHP通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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