How does php handle notice error?

The following code generates a notice if $n is not set. Solving it requires an additional statement (isset($n)) or to "declare" the $n ($n=''). But what consequences does this notice have? The below code is a lot neater and lets say we turn error_reporing off in production no difference is visible frontend. Does something bad follows? Prestanda, readability etc? (sorry for the bad english)

if($n==1){
   //do something
}

asked May 7, 2011 at 12:19

There is no "consequence" to notices, per sé, other than bad coding practices. You should be coding with error_reporting set to E_ALL on your development machines, so obviously the consequence there is a lot of notices...

I would argue that your code actually isn't neat, because you're testing a variable which doesn't get set previously.

A suggestion would be something like this:

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

pavium

14.4k4 gold badges31 silver badges48 bronze badges

answered May 7, 2011 at 12:24

Jim RubensteinJim Rubenstein

6,8164 gold badges35 silver badges54 bronze badges

2

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

Therefore a notice by itself doesn't mean that something bad happens most of the time. However, I wouldn't call anyone who accepts notices in their code a professional programmer because fixing the notices is a pretty simple task and not having any notices says that you understand the language's basics well. If someone can't or don't want to do even this much, it says something about them.

In your specific example, something like this should be done:

$n = null; // or some other appropriate initial value

// possibly change the value of $n here

if($n==1) {
    //do something
}

Note that by writing the extra $n = null, you are not making the program any different as far as the compiler is concerned (it will end up doing that itself at the same time it gives out the notice anyway). But you are making it very different as far as someone reading the code is concerned: with this code they won't have a "WTF did this $n come from???" moment.

answered May 7, 2011 at 12:26

How does php handle notice error?

JonJon

418k78 gold badges722 silver badges793 bronze badges

1

Normally in the production environments all error reporting which come strait from PHP libraries is turned off or parsed before showing to end user (it`s still logged).

There are no consequences in notice, it`s just notice to developer that something bad could happen in this place, in your example initializing the variable with value.

answered May 7, 2011 at 12:27

I encountered one function that handling "PHP Notice" can be beneficial.

The function is:

geoip_record_by_name()

This function return "false" and send "PHP Notice" on IP's that do not find in its database. The standard is that IP's reserved for internal networks, or Localhost will not be found. As a horrible practice this function treat this normal condition as bed coding. WRRRRR!!!

There is solution to filter local IP,s before sending to this function ( assuming that all other addresses are covered by geoip database).

I consider this geoip_record_by_name() as pest function that handling of "PHP Notice" is justified.

Discussion related to this pest function

answered Jan 21, 2014 at 4:04

How does php handle notice error?

0

Is notice an error in PHP?

Notice Error Notice errors are minor errors. They are similar to warning errors, as they also don't stop code execution. Often, the system is uncertain whether it's an actual error or regular code.

Why does PHP not show errors?

If your PHP script is halting (or not executing code like you expect) and not displaying any error messages even though you know there's an error happening, it's likely that the 'display_errors' configuration setting is switched off.

Does PHP support error reporting?

The error reporting function is a built-in PHP function that allows developers to control which and how many errors will be shown in the application. Remember, the PHP ini configuration has an error_reporting directive that will be set by this function during runtime. error_reporting(0);