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

2007-06-13 Thread Robert Cummings
On Wed, 2007-06-13 at 15:05 +0100, Richard Davey wrote:
> Hi Tijnema,
> 
> Wednesday, June 13, 2007, 2:42:28 PM, you wrote:
> 
> > Nice one, but you could also do it like this:
> 
> >  > $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?
> 
> Yup.. even nicer, I'm using that method now :)

It's terribly verbose and inefficient...



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.  |
`'

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



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

2007-06-13 Thread Richard Davey
Hi Zoltán,

Wednesday, June 13, 2007, 3:09:16 PM, you wrote:

> 2007. 06. 13, szerda keltezéssel 15.42-kor Tijnema ezt írta:
>> On 6/13/07, Richard Davey <[EMAIL PROTECTED]> 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?
>> > >>
>> > >> > > >> $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:
>> 
>> > $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

Your solution was far better than my original, but I'm only ever
setting 1 value ($filter['flags']) at the end of each function, so
using the above technique will work for my requirements. Thank you
both for your suggestions.

Cheers,

Rich
-- 
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"

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



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

2007-06-13 Thread Richard Davey
Hi Tijnema,

Wednesday, June 13, 2007, 2:42:28 PM, you wrote:

> Nice one, but you could also do it like this:

>  $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?

Yup.. even nicer, I'm using that method now :)

Cheers,

Rich
-- 
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"

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