PHP Difference Between Notice and Warning

PHP difference between notice and warning

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.

Should PHP 'Notices' be reported and fixed?

Errors are errors. They have to be fixed before your code works.

Warnings are warnings. They warn you that what you're doing is probably a bad idea, even if it works (or seems to work) for you at the moment. So they too should probably be fixed.

Notices are notices. They should be noticed. Hence the name. It may not be a problem that your code generates some, but it's something you should examine and judge on a case-by-case basis.

And of course, it is much easier to notice notices if you don't get 400 of them. So there's a big benefit to trying to eliminate them. It makes the ones you haven't yet noticed become more noticeable.

Warning or Notice not shown

I found something in the documentation:

Note:

Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.

So it seems to be by design, although it's not clear why. Personally I would at least expect an E_NOTICE.

There is a link to a bug report in the comments of that page, which in turn is marked as duplicate of another bug. This one seems to be neither fixed nor closed. So maybe it will be changed/fixed in the future.

CakePHP warning and Notice after installation

If you look at your first warning/error message it should be clear what the issue is: "Warning (2): Missing argument 1 for View::element()".

Look on line 61 of your default layout View template (/app/View/Layouts/default.ctp). You obviously have a call to $this->element() that isn't passing a template name (hence Cake is looking for "Elements/.ctp").

Make sure you pass a template name to the element() method or remove it from your template. For example, if you want to include the template "View/Elements/site_header.ctp":-

echo $this->element('site_header');

The template just needs to exist in the 'View/Elements' folder. You don't need to pass the '.ctp' extension to the element() method, Cake assumes this.

Make sure you've read the docs on Elements.

Stop script execution upon notice/warning

Yes, it is possible. This question speaks to the more general issue of how to handle errors in PHP. You should define and register a custom error handler using set_error_handlerdocs to customize handling for PHP errors.

IMHO it's best to throw an exception on any PHP error and use try/catch blocks to control program flow, but opinions differ on this point.

To accomplish the OP's stated goal you might do something like:

function errHandle($errNo, $errStr, $errFile, $errLine) {
$msg = "$errStr in $errFile on line $errLine";
if ($errNo == E_NOTICE || $errNo == E_WARNING) {
throw new ErrorException($msg, $errNo);
} else {
echo $msg;
}
}

set_error_handler('errHandle');

The above code will throw an ErrorException any time an E_NOTICE or E_WARNING is raised, effectively terminating script output (if the exception isn't caught). Throwing exceptions on PHP errors is best combined with a parallel exception handling strategy (set_exception_handler) to gracefully terminate in production environments.

Note that the above example will not respect the @ error suppression operator. If this is important to you, simply add a check with the error_reporting() function as demonstrated here:

function errHandle($errNo, $errStr, $errFile, $errLine) {
if (error_reporting() == 0) {
// @ suppression used, don't worry about it
return;
}
// handle error here
}

How do I turn off PHP Notices?

You can disable notices by setting error reporting level to E_ALL & ~E_NOTICE; using either error_reporting ini setting or the error_reporting() function.

However, notices are annoying (I can partly sympathize) but they serve a purpose. You shouldn't be defining a constant twice, the second time won't work and the constant will remain unchanged!



Related Topics



Leave a reply



Submit