Re: Need a more elegant way of bitwise ORing values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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;

greets
Zoltán Németh

>     $c = '$c = ' . implode('|', $flags) . ';';
>     eval($c);
>     $filter['flags'] = $c;
> }
> ?>
> 
> The code checks for 3 booleans ($allow_fraction, etc) and if true it
> adds the const to the $flags array. At the end each value in the array
> is joined together into a string (the implode part), which would give
> something like: 4096|8192 - but I then need to return the actual value
> from this operation, so I'm eval'ing it.
> 
> I don't like using eval, so can anyone think of a better way to do
> this? All I need as an end result if the product of the bitwise
> operation (i.e. an integer) stored in $filter['flags']
> 
> 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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux