Question

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

Was it helpful?

Solution

It is the bitwise and operator.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top