PHP Notice - ignore or stop from happening?

greg's picture

He has: 1,581 posts

Joined: Nov 2005

When a variable is not set and is checked for matching data, such as if($variable == "text"), a PHP notice (undefined variable) is returned.

I have a security check where it is possible and fine that a var might not be set, but the point of the check is to only perform an action if the var is set to something specific.
As such, I don't bother with additional PHP code such as if(isset($variable))

Usually the PHP error settings are a little less strict and don't return these in the error logs.

But if PHP notices are set to be logged in a file, is the opening, appending and closing of a file more resource than a bit of PHP code checking if the var is set?

What about when PHP is set to not log these errors? Do we just allow it to silently ignore the issue?

What are your thoughts and general practice with this?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

In a talk given at Drupalcon, Rasmus Lerdorf briefly mentions that regardless of how the error reporting level is set, PHP still builds the error string. So you will waste a bit of time doing a bunch of string concats. As long as your error reporting level is set low enough, I don't think PHP pops open the error log.

It doesn't bother me, though. Smiling

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

IMO, it is best to write with no notices. however, you could do the following where you KNOW it is not going to cause a problem:

if (@($variable == "text"))

The @ tells it to suppress any warnings/notices.

-Greg

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Why do you think checking if a variable is set is inefficient, or will be less efficient than suppressing a warning? Are you checking millions or thousands of variables per cycle, or a dozen, or one?

Sounds like you're over-optimising, and prematurely. I would aim for clarity: explicitly checking if the variable is set documents for readers the fact that the variable is not always expected to be set. Omitting the else-branch documents the fact that its absence is unimportant.

Using @ might also work, but it's a blanket suppression, rather than specifying the single condition you care about.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I believe that always initializing your variables is a good habit. It's probably a left-over habit from register globals, but there is still the possibility that the same variable was used somewhere else.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Hmm, interesting comments!

Abhishek Reddy wrote:
Sounds like you're over-optimising, and prematurely.
You were advising me to future proof mysql in another thread, even where I'd said it wasn't necessary. I would have thought coding PHP to best practice and getting the maximum efficiency is also paramount.
I don't really know the efficiency, which is why I asked.

One var is in a header included in all pages, and while I know this one particular instance isn't going to cause any great upset to resources, when this is done with many variables throughout the site, I presumed it can add up.

And I am investigating everything possible in learning to maximise efficiency in all code. I just wondered what sort of impact this may have - especially when multiple instances.

Using @ sounds like a good option in some cases - of course only when the var is known to only return notices, and not errors or warnings. As suppressing error logs is a bad thing of course.

Initializing vars is also another good option, but this adds to the discussion really.
As my particular var in question doesn't get used unless it is something specific, initialising the var to null means means it is actually been set.
Again, one var isn't an issue, but when you need to initialise hundreds of vars throughout the site, it can add up. Especially when a lot of them might not be used, if they are in an if/else, include file etc.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

greg wrote:
Abhishek Reddy wrote:
Sounds like you're over-optimising, and prematurely.
You were advising me to future proof mysql in another thread, even where I'd said it wasn't necessary. I would have thought coding PHP to best practice and getting the maximum efficiency is also paramount.
I don't really know the efficiency, which is why I asked.

What I suggested regarding SQL wasn't an optimisation; it was good design. The idea is closer to writing safe and readable code as I suggested here, than it is to squeezing out nanoseconds of speed in a few request cycles.

Of course, one consequence of good design is typically some performance benefit, which I pointed out in the other thread. However, that doesn't mean every technique for improving performance is necessarily due to good design (it rarely is).

Also, speed (time complexity) is only one dimension. You have to consider space complexity, structural complexity, readability, safety, and so on, too.

greg wrote:
One var is in a header included in all pages, and while I know this one particular instance isn't going to cause any great upset to resources, when this is done with many variables throughout the site, I presumed it can add up.

That is a fine presumption for a hypothesis. I encourage you to test it experimentally before worrying about the real cost.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.