How To Handle PHP Notice Error?

A notice means that while your code will work as expected, it isn't written "like it should be". It's like the compiler telling you "I know what you mean here and I can do it, but you shouldn't rely on this. Please write it differently so I don't have to make assumptions".
The following code generates a notice if $n is not set.
if($n==1)
{
   //do something
}
Therefore a notice by itself doesn't mean that something bad happens most of the time. However, fixing the notices is a pretty simple task.

A solution or suggestion would be something like this:
if (!empty($n) && $n == 1)
{
    //do something
}
empty checks for existence automatically (just like calling isset before it) but it also checks to make sure your value doesn't evaluate as false with values like false, 0, or '' (empty string).

Source: http://stackoverflow.com/a/5921010

Comments

Popular Posts