简体   繁体   中英

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;
}

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

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

& is the and bitwise operato r.

$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 .

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.

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.

& 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

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';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM