PHP 与有条件的

发布于 2024-08-10 09:55:58 字数 1037 浏览 5 评论 0原文

我正在网上搜索随机密码生成器,我

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

?>

http 中 发现了这段代码://www.webtoolkit.info/php-random-password-generator.html

我只想问什么&方法?这是一个合乎逻辑的 AND 他只是忘记添加另一个 & 吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄居者 2024-08-17 09:55:58

不,它是一个按位与。强度被用作标志,对于每个位设置,辅音都会获得额外的设置与其连接的字符。

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.

泪冰清 2024-08-17 09:55:58

它是按位和运算符。

It is the bitwise and operator.

野侃 2024-08-17 09:55:58

单个“&”运算符是按位与。他将 $strength 与二进制表示形式 1、2、4,然后是 8 进行 AND 运算。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文