RE: Re[6]: [PHP] Need a more elegant way of bitwise ORing values
| From: | Ford, Mike | Date: | Wed, 13 Jun 2007 17:38:23 +0000 |
| Subject: | RE: Re[6]: [PHP] Need a more elegant way of bitwise ORing values | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-256894@lists.php.net to get a copy of this message | ||
________________________________
> From: Richard Davey [mailto:rich@corephp.co.uk]
> Sent: Wed 13/06/2007 15:19
> To: PHP List
>
> > Hi Robert,
>
> Wednesday, June 13, 2007, 3:15:39 PM, you wrote:
>
> > It's terribly verbose and inefficient...
>
> > <?php
>
> > $filter['flags'] = 0;
>
> > if( $allow_fraction )
> > {
> > $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION;
> > }
>
> > if( $allow_thousand )
> > {
> > $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND;
> > }
>
> > if( $allow_scientific )
> > {
> > $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC;
> > }
>
> ?>>
>
> I don't think it's *terribly* verbose, as it has good sentence structure
> to it, but your version is certainly more efficient, hence I've
> swapped to that. Any other takers? ;)
Well, I don't know about more efficient but I'd be terribly tempted to express it like
this:
$filter['flags'] = $allow_fraction ? FILTER_FLAG_ALLOW_FRACTION : 0
| $allow_thousand ? FILTER_FLAG_ALLOW_THOUSAND : 0
| $allow_scientific ? FILTER_FLAG_ALLOW_SCIENTIFIC : 0;
Whether you think this is more readable and/or less verbose probably depends on personal taste! ;)
---------------------------------------------------------------------
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: m.ford@leedsmet.ac.uk
Tel: +44 113 812 4730 Fax: +44 113 812 3211
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm