Re: Notices
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Tue, 12 Aug 2008 21:09:29 -0400
Jeff wrote:
Jerry Stuckle wrote:Jeff wrote:Jerry Stuckle wrote:
Hello Jerry,
Jeff wrote:I'm still coming to grips with Notices, particularly on missing indexes.
Lets say we have a form full of check boxes. An unchecked checkbox is not sent in $_REQUEST and if you do something with $_REQUEST['my_checkbox'] you'll get a notice. Is there a clean method of dealing with this?
I'm a recovering perl programmer so conciseness is still important. I suppose I'll get over it eventually but I still miss ||= .
Jeff
You should NEVER configure a development server to ignore notices. Each one is a potential bug in your program - which is why they are being displayed. They are notices to let you know there MAY be a problem. A good programmer will fix these notices.
OK. I'm working my way through the php way of error checking. It's enough unlike perl that I have a hard time understanding some bits.
http://us.php.net/manual/en/errorfunc.constants.php#errorfunc.constants.errorlevels.e-user-warning
I'm not sure how to use that. What if I wanted to suppress notices and look at everything else? It's a bit tedious at times to fix harmless missing index notices when you just want to test something.
It's simple boolean algebra: error_reporting = E_ALL & ~ E_NOTICE
But missing index notices are NEVER harmless! They may not cause a problem now. But they will later.
I remain skeptical that an undef has more harmful potential than an empty string.
Well, for one thing, if you're setting it to an empty string, then it is set - no matter what the previous value might have been. But if you don't set it, what happens when some other code which might be added later sets that variable to something you don't expect?
Also, uninitialized variables are symptomatic of sloppy programming.
But I see that this missing index Notice clutter obscures other potential problems. I also see that it is much harder to fix the clutter after you've written the code. So, I think it's good advice to "fix" these as you go and that's what I will do.
Yes, that's one problem.
Jeff
Also, let's say we had a file open that I didn't care if it failed. I just wrote this:
$fh = @fopen($file_path, 'w');
if($fh){
fwrite($fh, $file_data);
fclose($fh);
}
Now, $fh is not a boolean yet I don't get a notice on that. What should I use to test for a resource?
Look at fopen() and see what it returns. Per the doc:
"Returns a file pointer resource on success, or FALSE on error."
It's not just boolean variables which can be true or false. (Numeric) 0 or an empty string will also evaluate to false. But a valid resource will never evaluate to false.
And it's easy to do. Just use isset() to determine of the checkbox is set, or not, i.e.
$my_checkbox = isset($_POST['my_checkbox']) ? $_POST['my_checkbox'] : '';
I like that syntax.
The last value can be an empty string, null, 0 or whatever you want for a value when the checkbox is not set.
Also, you should always user $_POST or $_GET instead of $_REQUEST. $_REQUEST gets its data from $_GET, $_POST or $_COOKIE, and can too easily cause problems (i.e. what happens if two years from now, somewhere else in your program, you call setcookie('my_checkbox', 1)?
This code could break, among other things.
I understand that. For good and bad, it does cover all the bases. Is there a set order that $_REQUEST looks through, Cookies before/after Post before after Get?
Jeff
Whatever's define in your php.ini file. But no matter what order it looks through - if the variable is defined in ANY of them, it will be defined.
You should ALWAYS use the correct source array - $_GET, $_POST or $_COOKIE - to fetch your value.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- References:
- Notices
- From: Jeff
- Re: Notices
- From: Jerry Stuckle
- Re: Notices
- From: Jeff
- Re: Notices
- From: Jerry Stuckle
- Re: Notices
- From: Jeff
- Notices
- Prev by Date: Re: Symbol that rotetes text.
- Next by Date: Re: setting filemtime
- Previous by thread: Re: Notices
- Next by thread: Re: Notices
- Index(es):
Relevant Pages
|