Error notices and reporting

Started by Peter Duggan, December 05, 2004, 03:02:57 PM

Previous topic - Next topic

Peter Duggan

So I've got a script which works with a lot of $_POST variables running under error_reporting (E_ALL ^ E_NOTICE), try reverting to error_reporting(E_ALL), bring my local install of Apache to its knees with a string of 'undefined index' errors and decide it would be quicker to stop and restart the server than wait!

Now I know I can stop this completely by pre-testing everything with isset() instead of just listing $blah = $_POST('blah'), but is this the best thing to do? What are the pros and cons of doing that instead of just leaving things as they are when the PHP manual explicitly defines E_NOTICE as:

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

?


[Unknown]

You could use:

$blah = @$_POST['blah'];
$blah = isset($_POST['blah']) ? $_POST['blah'] : '';

-[Unknown]

Peter Duggan

Quote from: [Unknown] on December 05, 2004, 05:30:56 PM
You could use:

$blah = @$_POST['blah'];
$blah = isset($_POST['blah']) ? $_POST['blah'] : '';

So the latter is what I meant by 'stop this completely by pre-testing everything with isset()' and the former also makes sense, but is there anything else I should know?

Like do you do either or both of these (and why), is suppressing the errors with @ in any way better than using (E_ALL ^ E_NOTICE) and is there any real disadvantage to just leaving things as they are?

Thanks
Peter

[Unknown]

Well, you see, one thing to note is that every time a notice gets sent off, it bounces around inside the innard's of PHP for a bit.  Meaning, notices are slow.  I try to avoid them.  However, the if or ternary construct there isn't really faster either... so using @ or isset doesn't make the hugest difference.

However, notices are used for other things.  Example:

$this_is_a_variable = 2;
echo $this_is_a_varaible;

At first glance, you may not see the problem.  But a notice will - you spelled variable wrong the second time.  There are many other things they are useful for, too.

I always write code with notices on.  In fact, I've taken other software (Mambo, other forums, etc.) and turned on E_ALL... and found legitimate bugs (not just things like the @/isset would fix, but things that were actually working incorrectly.)  A lot of people don't realize this, and think that it's "too strict".... I can only disagree.

-[Unknown]

Peter Duggan

Right, so if I've got this right:

1. The notices I got about the $_POST variables are not in themselves harmful.
2. Suppressing them with @ means I'll still be notified about mistyped variables and stuff.
3. Testing the variables with the ternary operator might not slow things down any more than leaving the notices to 'bounce around' for a bit.

Which is basically what I wanted to know but was having trouble establishing from Google/php.net. And, since I remembered you saying something like 'all errors are bad' or 'errors are always bad' (not located by search), I thought I'd ask! ;)

[Unknown]

Quote from: Peter Duggan on December 05, 2004, 06:00:31 PM
2. Suppressing them with @ means I'll still be notified about mistyped variables and stuff.
3. Testing the variables with the ternary operator might not slow things down any more than leaving the notices to 'bounce around' for a bit.

Well, errors are always bad, but my point with these two was that @ still has the error bounce around, meaning that there's no *huge* difference between 2 and 3.

Then again, if you want a $_POST variable sent, and it must be, I would recommend not using @ or isset - assume it is, and then log any notices you might get.

-[Unknown]

Peter Duggan

Quote from: [Unknown] on December 05, 2004, 08:05:30 PM
Then again, if you want a $_POST variable sent, and it must be, I would recommend not using @ or isset - assume it is, and then log any notices you might get.

Not quite sure why you're suggesting logging when I already know why I'm getting the notices, so perhaps I'd better explain that these variables have been 'set up' before the forms that supply the data, like:

$this = trim($_POST['this']);
$that = htmlentities($_POST['that']);
and so on...

Whereas your last reply suggests you might be assuming they were being declared after the $_POST data had been collected, in which case we could just be talking at cross-purposes?

So the notices are because there's no $_POST['anything'] when the variables are first declared, and my initial question (about 'pre-testing everything with isset') should be taken in that context.

Peter Duggan

Quote from: Peter Duggan on December 05, 2004, 03:02:57 PM
So I've got a script which works with a lot of $_POST variables running under error_reporting (E_ALL ^ E_NOTICE), try reverting to error_reporting(E_ALL), bring my local install of Apache to its knees with a string of 'undefined index' errors and decide it would be quicker to stop and restart the server than wait!

Quote from: [Unknown] on December 05, 2004, 05:44:48 PM
I always write code with notices on. In fact, I've taken other software (Mambo, other forums, etc.) and turned on E_ALL... and found legitimate bugs (not just things like the @/isset would fix, but things that were actually working incorrectly.) A lot of people don't realize this, and think that it's "too strict".... I can only disagree.

Having seen first hand what any quantity of 'undefined index' and 'undefined variable' errors can do to my perfectly respectable test server (and subsequently spent this evening overhauling a fairly complex script to meet the requirements of E_ALL), I can only agree with [Unknown]'s assessment here. But I must also stress that local testing is vital because, despite not finding anything more serious wrong with my code, I'd hate to think what might have happened if I'd started uploading this stuff straight to the web in the more severe mode!

Elijah Bliss

uh boy.

Here's my question:
I am finishing up a mod for SMF and so far things are working, and I'm not getting any error messages...
until I take a peek at my error log. I am getting a plethora of "Undefined Indexes and Undefined Variables"

Is there something here I am missing in terms of mod development or should I suppress all of my $_POST variables with @?

Ben_S

SMF hides warnings, but logs them (as you have noticed). @ hides the error message, but the error is still there, you should ideally correct the error.
Liverpool FC Forum with 14 million+ posts.

Elijah Bliss

Quote from: Ben_S on February 02, 2005, 08:54:13 PM
SMF hides warnings, but logs them (as you have noticed). @ hides the error message, but the error is still there, you should ideally correct the error.

Fixed. The proper way, w/o hiding. Took me 2 hours worth of research off the net to understand what I was doing wrong...

now should I chuck this Larry Ullman PHP Book in to the trash?

[Unknown]

Quote from: Elijah Bliss on February 02, 2005, 11:32:54 PM
now should I chuck this Larry Ullman PHP Book in to the trash?

That's what I don't like about most of the more common programming books; they teach it *wrong.*

-[Unknown]

Elijah Bliss

Quote from: [Unknown] on February 03, 2005, 03:41:43 AM
Quote from: Elijah Bliss on February 02, 2005, 11:32:54 PM
now should I chuck this Larry Ullman PHP Book in to the trash?

That's what I don't like about most of the more common programming books; they teach it *wrong.*

-[Unknown]

I went to his website to see what he had to write about the errors:
http://www.dmcinsights.com/phorum/read.php?9,13172,13179#msg-13179

"Undefined index errors can be dismissed by changing the level of error reporting."

Best Wishes,
Larry

Obviously he doesn't take them as seriously as you guys do. I would like to call him out on it seeing that I purchased his book (which came highly recommended, even from this forum) and used his tutorials to create my mod. Unfortunately I don't know enough to make a sharp criticism, but I come from the old school of "Do it right the first time" and if this guy isn't teaching it the proper way, he should be called out on it.

[Unknown], have you written any books or can you recommend any books (beginner's preferably) that teaches PHP properly?

[Unknown]

No, I'm afraid not.  I'm a horrible writer and I only read sci-fi :P.

-[Unknown]

Peter Duggan

Quote from: Elijah Bliss on February 02, 2005, 11:32:54 PM
Quote from: Ben_S on February 02, 2005, 08:54:13 PM
SMF hides warnings, but logs them (as you have noticed). @ hides the error message, but the error is still there, you should ideally correct the error.

Fixed. The proper way, w/o hiding. Took me 2 hours worth of research off the net to understand what I was doing wrong...

Quote from: Elijah Bliss on February 03, 2005, 06:08:06 AM
I went to his website to see what he had to write about the errors:
http://www.dmcinsights.com/phorum/read.php?9,13172,13179#msg-13179

"Undefined index errors can be dismissed by changing the level of error reporting."

Best Wishes,
Larry

Obviously he doesn't take them as seriously as you guys do.

Hey, it's good to see another convert! :)

Advertisement: