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

2007-06-13 Thread Robert Cummings
On Thu, 2007-06-14 at 00:33 +0100, Richard Davey wrote:
> Hi Richard,
> 
> Wednesday, June 13, 2007, 6:44:55 PM, you wrote:
> 
> >> if ($allow_fraction)
> 
> > //Should we warn you that $allow_fraction is not actually defined?...
> 
> Should I warn you that to save everyone's sanity I only posted what was
> needed from the code? ;) $allow_fraction came from a function
> parameter. I'd type hint it to boolean if PHP allowed, but alas ...
> 
> > $flags |= FILTER_FLAG_ALLOW_FRACTION;
> 
> Yup, Robert showed this already in this thread. Nice short-cut, wasn't
> aware it worked with that operator. That's definitely my "learnt
> something new" for today. I wonder what else it works for? (besides
> the obvious += -=)

http://ca3.php.net/manual/en/language.operators.php

There's quite a few.

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[2]: [PHP] Need a more elegant way of bitwise ORing values

2007-06-13 Thread Richard Davey
Hi Richard,

Wednesday, June 13, 2007, 6:44:55 PM, you wrote:

>> if ($allow_fraction)

> //Should we warn you that $allow_fraction is not actually defined?...

Should I warn you that to save everyone's sanity I only posted what was
needed from the code? ;) $allow_fraction came from a function
parameter. I'd type hint it to boolean if PHP allowed, but alas ...

> $flags |= FILTER_FLAG_ALLOW_FRACTION;

Yup, Robert showed this already in this thread. Nice short-cut, wasn't
aware it worked with that operator. That's definitely my "learnt
something new" for today. I wonder what else it works for? (besides
the obvious += -=)

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: Re[2]: [PHP] Need a more elegant way of bitwise ORing values

2007-06-13 Thread Zoltán Németh
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

greets
Zoltán Németh

> 
> Tijnema

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



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

2007-06-13 Thread Tijnema

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:



Little bit simpler huh?

Tijnema

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



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

2007-06-13 Thread Richard Davey
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
-- 
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