Can anyone translate this to english for me

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ellisje22
Forum Newbie
Posts: 1
Joined: Tue Dec 29, 2009 3:27 pm

Can anyone translate this to english for me

Post by ellisje22 »

I am a java programmer, tasked with reverse engineering some PHP code.

Can anyone translate the following function for me into English that I can understand?

Code: Select all

private function _getserver($key) 
    {
        $bucket=((crc32($key)>>16)&0x7fff) % $this->bucketcount;
        $tries=$this->bucketcount;
        
        while($tries)
        {
            $mc=@memcache_connect(get_cfg_var("servername.memcache.ip".($bucket+1)),get_cfg_var("servername.memcache.port".($bucket+1)));
            if($mc)
            {
                return $mc;
            }
            $bucket=($bucket+1)%$this->bucketcount;
            --$tries;
        }
        
        return null;
    }

I am particularly confused by the line:

Code: Select all

$bucket=((crc32($key)>>16)&0x7fff) % $this->bucketcount;
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Can anyone translate this to english for me

Post by AbraCadaver »

crc32($key)
calculate the checksum polynomial of 32-bit lengths of $key

>>16
shift the bits 16 steps to the right

&0x7fff
perform a bitwise AND with hex 7fff

%
divide that by $this->bucketcount and return the remainder (modulo)

Finally, assign the result to $bucket
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply