Re: Re[6]: [PHP] Need a more elegant way of bitwise ORing values
| From: | Robin Vickery | Date: | Sun, 17 Jun 2007 16:00:57 +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-257214@lists.php.net to get a copy of this message | ||
On 13/06/07, Richard Davey <rich@corephp.co.uk> wrote:
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 ) {lose the conditionals? $filter['flags'] = 0; $allow_fraction && $filter['flags'] |= FILTER_FLAG_ALLOW_FRACTION; $allow_thousand && $filter['flags'] |= FILTER_FLAG_ALLOW_THOUSAND; $allow_scientific && $filter['flags'] |= FILTER_FLAG_ALLOW_SCIENTIFIC; or keep the conditionals and just do one assignment? $filter['flags'] = ($allow_fraction ? FILTER_FLAG_ALLOW_FRACTION : 0) | ($allow_thousand ? FILTER_FLAG_ALLOW_THOUSAND : 0) | ($allow_scientific ? FILTER_FLAG_ALLOW_SCIENTIFIC : 0); as the perl motto says, "there's more than one way to do it". -robin -robin$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? ;)