开发者

PHP difference between notice and warning

开发者 https://www.devze.com 2023-02-03 13:31 出处:网络
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

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 noti开发者_开发技巧ce 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.


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.


Differences are explained here: 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.


  • 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.


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 or notices.

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.


  • 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.


According to PHP Manual:

  • 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号