通知和警告之间的 PHP 区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4624474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:44:19  来源:igfitidea点击:

PHP difference between notice and warning

phperror-handling

提问by Rene Terstegen

When writing code errors, warnings and notices can occur. I know the idea behind errors. I suppose a warning is there to inform you about something that can cause an error, but isn't a notice exaclty the same? I suppose a notice is not a message of something doing right ;).

编写代码错误时,可能会出现警告和通知。我知道错误背后的想法。我想有一个警告是为了通知您可能导致错误的事情,但通知不一样吗?我想通知不是做正确事情的消息;)。

It's just a bit confusing to me. Can anybody tell the difference between those two and the way these messages should be treated.

这对我来说有点混乱。任何人都可以说出这两者之间的区别以及应该如何处理这些消息。

回答by Jacob Relkin

A notice is an advisory message meaning "You probably shouldn't be doing what you're doing, but I'll let you do it anyway"

通知是一条建议性消息,意思是“你可能不应该做你正在做的事情,但无论如何我会让你做”

A warning is a message saying "You are doing something wrong and it is very likely to cause errors in the future, so please fix it."

警告是一条消息,表示“您做错了事情,将来很可能会导致错误,因此请修复它。”

Both notices and warnings will not halt execution of your script, although I would encourage you to take them seriously and strive to have not even one notice in your apps.

通知和警告都不会停止您的脚本的执行,尽管我鼓励您认真对待它们并努力在您的应用程序中没有任何通知。

回答by Mchl

Differences are explained here: http://www.php.net/manual/en/errorfunc.constants.php

这里解释了差异:http: //www.php.net/manual/en/errorfunc.constants.php

Specifficaly:

具体来说:

Errors:

错误:

Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.

致命的运行时错误。这些表示无法恢复的错误,例如内存分配问题。脚本的执行被暂停。

Warnings:

警告:

Run-time warnings (non-fatal errors). Execution of the script is not halted.

运行时警告(非致命错误)。脚本的执行不会停止。

Notices:

注意事项:

Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.

运行时通知。表示脚本遇到了一些可能表示错误的事情,但也可能在运行脚本的正常过程中发生。

回答by vikash

  • NOTICE: this is a short message for saying what to do or what not to do.
  • WARNING: occcurs at run time. Code execution continues.
  • ERROR: this also occurs at run time, execution terminates.
  • 注意:这是一条短消息,用于说明该做什么或不该做什么。
  • 警告:在运行时发生。代码执行继续。
  • 错误:这也发生在运行时,执行终止。

回答by jai

  • NOTICE: It is a message for saying what you should do and what you should not do.
  • WARNING: It occurs at run time.But it do not interrupt Code execution.
  • ERROR: It also occurs at run time, but program execution is not continued it terminates.
  • 注意:这是一条信息,用于说明您应该做什么和不应该做什么。
  • 警告:它发生在运行时。但它不会中断代码执行。
  • 错误:它也发生在运行时,但程序执行不会继续它终止。

回答by Shabbyrobe

I won't re-iterate the specific meanings, which have been thoroughly covered by other answers.

具体含义我就不重复了,其他答案已经完全涵盖了。

Warnings and notices indicate different "badness" levels for things you might be doing wrong, but I'm with the PEAR/Zend guides on this: you should always code to E_STRICT, which means you shouldn't ever raise either warnings ornotices.

警告和通知为您可能做错的事情指明了不同的“不良”级别,但我支持 PEAR/Zend 指南:您应该始终编码为 E_STRICT,这意味着您不应该发出警告通知。

If you follow E_STRICT compatibility, the distinction between notices and warnings is kind of redundant if both are considered "Non-fatal errors that you should make sure don't happen" - they're both essentially warnings.

如果您遵循 E_STRICT 兼容性,如果两者都被视为“您应该确保不会发生的非致命错误”,则通知和警告之间的区别有点多余 - 它们本质上都是警告。

回答by Kel

According to PHP Manual:

根据PHP 手册

  • WARNING- Run-time warnings (non-fatal errors). Execution of the script is not halted.
  • NOTICE- Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
  • 警告- 运行时警告(非致命错误)。脚本的执行不会停止。
  • 注意- 运行时通知。表示脚本遇到了一些可能表示错误的事情,但也可能在运行脚本的正常过程中发生。

Personally I think, that notices indicate some code parts, which potentially can lead to some problems, but which were introduced intentionally. In such cases programmer is considered to "know what he is doing" and to know about some particular PHP features (type casts, default initialization values etc).

我个人认为,通知表明了一些代码部分,这可能会导致一些问题,但这些部分是故意引入的。在这种情况下,程序员被认为“知道他在做什么”并了解一些特定的 PHP 特性(类型转换、默认初始化值等)。