M. of CA
M. of CA

Reputation: 1546

PHP if & question

what does this mean "if ($strength & 2) {" in the following: I dont understand the $ 2 part...

    function generatePassword($length=11, $strength=7) {
    $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;
}

Upvotes: 0

Views: 482

Answers (7)

Prabakaran R
Prabakaran R

Reputation: 11

You have used $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; to change $vowels = 'aeiou'; $consonants = 'bdghjmnpqrstvyz';

Upvotes: 1

The Mask
The Mask

Reputation: 17427

the '&' is the a bitwise operator. returns 1 not a boolean. Well,if you are looking do a comparison use the "==" operator.

replace

if ($strength & 1) {
    $consonants .= 'BDGHJLMNPQRSTVWXZ';
}

by

if ($strength == 1) {
    $consonants .= 'BDGHJLMNPQRSTVWXZ';
}

Upvotes: 0

bjornd
bjornd

Reputation: 22943

This if ($strength & 2) { means "if the second bit is equal to 1". Suppose $strength = 6 or 110 in binary system, at the same time binary representation of 2 is 10. So & operator does a bitwise and operation:

110
010
=
010

You get positive number only when the second bit of $strength is equal to 1, otherwise you get 0.

Upvotes: 1

alex
alex

Reputation: 490283

& is the and bitwise operator.

$a & $b Bits that are set in both $a and $b are set.

$strength appears to be an option for the strength of the password.

2 in binary is 00000010. If $strength was 2, then it would run that condition because the resulting number would be 2 (as both bits are set the same in both numbers).

It $strength were 1 (0000001), when bitwised with 2 would produce 0, and the condition would be false.

Upvotes: 1

ETWW-Dave
ETWW-Dave

Reputation: 732

It's a bitwise comparison - see http://www.litfuel.net/tutorials/bitwise.htm for a basic description of what is going on.

If the author had defined some nice constants, it would read more like:

function generatePassword($length = 11, $strength = STRENGTH_INCLUDE_UPPERCASE & STRENGTH_INCLUDE_DIGITS & STRENGTH_INCLUDE_SYMBOLS) {
  if ($strength & STRENGTH_INCLUDE_UPPERCASE) { ...
  if ($strength & STRENGTH_INCLUDE_DIGITS) { ...
  if ($strength & STRENGTH_INCLUDE_SYMBOLS { ...

which would make it far more readable for you and maintainable in the future.

Upvotes: 1

lunixbochs
lunixbochs

Reputation: 22415

& is a bitwise operator. It manipulates the physical bits of a number. & is known as "bitwise AND". Given two numbers, it will create a new number for all of the shared bits in both numbers.

If you line up the bits for two numbers, any matching bits will be in the resulting number.

 7: 00000111
 2: 00000010

 7 & 2 == 2

This is an easy way to store information in a compact manner.

Upvotes: 4

Tudor Constantin
Tudor Constantin

Reputation: 26861

& is a bitwise operator. Mainly it tells you if the binary form of $strength has a 1 on position:

  • 2 for 2
  • 3 for 4
  • 4 for 8

Upvotes: 0

Related Questions