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

From: Date: Wed, 13 Jun 2007 14:09:16 +0000
Subject: Re: Re[2]: [PHP] Need a more elegant way of bitwise ORing values
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-256858@lists.php.net to get a copy of this message
2007. 06. 13, szerda keltezéssel 15.42-kor Tijnema ezt írta: > On 6/13/07, Richard Davey <rich@corephp.co.uk> wrote: > > Hi Zoltán, > > > > Wednesday, June 13, 2007, 2:21:18 PM, you wrote: > > > > > 2007. 06. 13, szerda keltezéssel 14.13-kor Richard Davey ezt írta: > > >> Hi all, > > >> > > >> Can anyone think of a more elegant way of achieving the following? > > >> > > >> <?php > > >> $flags = array(); > > >> > > >> if ($allow_fraction) > > >> { > > >> $flags[] = FILTER_FLAG_ALLOW_FRACTION; > > >> } > > >> > > >> if ($allow_thousand) > > >> { > > >> $flags[] = FILTER_FLAG_ALLOW_THOUSAND; > > >> } > > >> > > >> if ($allow_scientific) > > >> { > > >> $flags[] = FILTER_FLAG_ALLOW_SCIENTIFIC; > > >> } > > >> > > >> if (count($flags) > 0) > > >> { > > > > > $tmp = FALSE; > > > foreach ($flags as $flag) { > > > $tmp = $tmp | $flag; > > > } > > > $filter['flags'] = $tmp; > > > > Nice one, thank you. Same amount of code but avoids the eval() which > > is all I wanted. > > > > Cheers, > > > > Rich > Nice one, but you could also do it like this: > > <?php > $filter['flags'] = FALSE; > > if ($allow_fraction) > { > $filter['flags'] = $filter['flags'] | FILTER_FLAG_ALLOW_FRACTION; > } > > if ($allow_thousand) > { > $filter['flags'] = $filter['flags'] | FILTER_FLAG_ALLOW_THOUSAND; > } > > if ($allow_scientific) > { > $filter['flags'] = $filter['flags'] | FILTER_FLAG_ALLOW_SCIENTIFIC; > } > > ?> > > Little bit simpler huh? in this case yes. but if he wants to use the $flags array to anything else besides creating $filter['flags'] from it, then my solution would be better greets Zoltán Németh > > Tijnema

« previous php.general (#256858) next »