To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



On 26 January 2005 19:56, [EMAIL PROTECTED] wrote:

> On 26 Jan 2005 Jason Barnett wrote:
> 
> > if (isset($_POST['checkboxfieldname'])) {
> >    /** do stuff */
> > }
> 
> Sorry, I should have mentioned that I knew about using isset -- it
> works OK for the checkbox example, though I'm not clear if this
> behavior is specified and therefore will not change -- i.e. can one
> rely on the fact that the checkbox field name is missing
> entirely from
> _POST if the box is not checked?  Or are there cases where it could be
> present but with an empty or NULL value?
> 
> If one must check the value and not just the existence of the checkbox
> entry, or for other uses, e.g. where a flag may or may not be present,
> one is saddled with clumsy constructs like:
> 
>       if (($isset($array['index']) && ($array['index'] == 1)) ...
> 
> I would prefer that the second expression return FALSE, or that
> $array['index'] where the index is not present simply return
> NULL -- or
> probably better, an option to avoid E_NOTICE level errors for such
> cases.

The @ operator exactly matches your requirement (and is also my personal
preference in this case):

    @$array['index']

will return NULL if either $array or $array['index'] doesn't exist, so:

    if (@$array['index'] == 1)

works as you want.

I've seen arguments against doing this that say (i) it's bad form to be
using error suppression just to change the result returned from an
expression, and (ii) the error-suppression operator is very slow.  My
answers to these are: (i) if the operator exists and does exactly what you
want, why the heck not use it?, and (ii) According to my benchmarks, on
average it's no slower than other solutions.

Anyway, I like the elegance of a single character operator over any of the
other more verbose possibilities.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to