views:

71

answers:

3

I was searching the net for a random password generator and I come across this code

<?php

function generatePassword($length=9, $strength=0) {
    $vowels = 'aeuy';
    $consonants = 'bdghjmnpqrstvz';
    if ($strength & 1) {
     $consonants .= 'BDGHJLMNPQRSTVWXZ';
    }
    if ($strength & 2) {
     $vowels .= "AEUY";
    }
    if ($strength & 4) {
     $consonants .= '23456789';
    }
    if ($strength & 8) {
     $consonants .= '@#$%';
    }

    $password = '';
    $alt = time() % 2;
    for ($i = 0; $i < $length; $i++) {
     if ($alt == 1) {
      $password .= $consonants[(rand() % strlen($consonants))];
      $alt = 0;
     } else {
      $password .= $vowels[(rand() % strlen($vowels))];
      $alt = 1;
     }
    }
    return $password;
}

?>

from http://www.webtoolkit.info/php-random-password-generator.html

and I would just like to ask what & means? Is that a logical AND did he just forget to add another &?

+3  A: 

It is the bitwise and operator.

Chris Kloberdanz
Can't believe I missed that, TY!
lemon
+4  A: 

No, it is a bitwise and. Strength is being used as flags, for each bit set, consonants gets an extra set of characters concatenated to it.

Evän Vrooksövich
+2  A: 

The single "&" operator is a bitwise and. He is AND-ing $strength with the binary representation of 1, 2, 4, and then 8.

FrustratedWithFormsDesigner