Re: Re[6]: [PHP] Need a more elegant way of bitwise ORing values

From: Date: Wed, 13 Jun 2007 14:37:38 +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-256870@lists.php.net to get a copy of this message
On Wed, 2007-06-13 at 15:19 +0100, Richard Davey 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 ) > > { > > $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? ;) Personally I hate constants (can't use non-scalar values so why get used ot them... also they're just another point for name collision) so if it were my own code I'd do something more like the following: <?php $GLOBALS['filterFlags'] = array ( 'allowFraction' => 1 << 0, 'allowThousand' => 1 << 1, 'allowScientific' => 1 << 2, ); // then your above code would become: $filter['flags'] = 0; foreach( $filterFlags as $name => $bits ) { if( isset( $$name ) && $$name ) { $filter['flags'] |= $bits; } } ?> Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#256870) next »