0

我试图创建一个只生成许可证密钥的程序。这里是:

<?php

function gen_code_alpha()
{
    $alpha = '';

    for ($i = 0; $i <= 9; $i++) {
        $alpha .= $i;
    }

    // This attaches alphabets from 'a' to 'z' to our $alpha
    for ($i = 65; $i <= 122; $i++) {
        $alpha .= chr($i);
    }
}

function gen_code($len = 1)
{
    gen_code_alpha();
    global $alpha;

    $strlen = strlen($alpha);

    $code = '';
    for ($k = 0; $k < $len; $k++) {
        $i = $rand(0, $strlen -1);// now wanna randomly generate the code
        $code .= substr($alpha, $i, 1);
    }

    return $code;
}

function gen_license_key()
{
    $licenseKey = gen_code(4) . '-' .
                  gen_code(4) . '-' .
                  gen_code(4) . '-' .
                  gen_code(4) . '-' .
                  gen_code(2);
}

gen_license_key();
echo $licenseKey;

但是当我尝试运行它时,它没有显示任何输出。我是 PHP 新手,对调试不太了解,但我尝试了一些。我在代码中放了一些回声,我知道我的 $alpha 是在 gen_code_alpha() 函数中成功生成的。我还尝试在没有帮助的 gen_license_key() 函数中回显 $licenseKey。我没有发现更多的东西。

所以你怎么看?

4

5 回答 5

2

你快到了,只需改变这个:

function gen_license_key()
{
    // return the result instead of assigning it to a local variable
    return gen_code(4) . '-' .
           gen_code(4) . '-' .
           gen_code(4) . '-' .
           gen_code(4) . '-' .
           gen_code(2);
}

然后你可以:

// show the key, do nothing else
echo gen_license_key();

// OR

// store the key in $licenseKey so you can do other stuff with it if needed
$licenseKey = gen_license_key();

// Now show the key
echo $licenseKey;

此外,global $alpha = '';在您的gen_code_alpha()函数中设置,或者我强烈建议您遵循“返回”模式来编写函数。

于 2018-08-06T18:05:04.850 回答
0

在函数内部定义的变量只存在于这些函数内部。您应该return从函数中获取值,而不是设置全局变量:

function gen_license_key()
{
    return gen_code(4) . '-' .
        gen_code(4) . '-' .
        gen_code(4) . '-' .
        gen_code(4) . '-' .
        gen_code(2);
}

然后,您可以通过将变量分配给函数返回的值来获取新值:

$licenseKey = gen_license_key();
echo $licenseKey;
于 2018-08-06T18:06:17.677 回答
0

这是完整的代码工作

请注意,您有一个错字(rand,而不是 $rand)

我还建议您更改代码以避免使用全局变量,这是一种不好的做法。

<?php

function gen_code_alpha()
{
    global $alpha;

    $alpha = '';

    for ($i = 0; $i <= 9; $i++) {
        $alpha .= $i;
    }

    // This attaches alphabets from 'a' to 'z' to our $alpha
    for ($i = 65; $i <= 122; $i++) {
        $alpha .= chr($i);
    }
}

function gen_code($len = 1)
{
    gen_code_alpha();
    global $alpha;

    $strlen = strlen($alpha);

    $code = '';
    for ($k = 0; $k < $len; $k++) {
        $i = rand(0, $strlen -1);// now wanna randomly generate the code
        $code .= substr($alpha, $i, 1);
    }

    return $code;
}

function gen_license_key()
{
    $licenseKey = gen_code(4) . '-' .
                gen_code(4) . '-' .
                gen_code(4) . '-' .
                gen_code(4) . '-' .
                gen_code(2);
    return $licenseKey;
}

$licenseKey = gen_license_key();
echo "l:".$licenseKey;

?>

输出:

 l:3X6D-gnQL-I0zJ-TRQD-Sq
于 2018-08-06T18:11:37.163 回答
0

如果您真的想走这条正确使用全局变量的道路,那么您必须先引用它们,如果您要分配它们,则必须尽早引用它们。此外,由于您似乎没有$alpha在这些函数之外使用,只需返回这些值并避免全局命名空间污染。

您还有一个错字,$rand不是变量的名称,而是rand()已更正的函数。

<?php
function gen_code_alpha()
{
    $alpha = '';

    for ($i = 0; $i <= 9; $i++) {
        $alpha .= $i;
    }

    // This attaches alphabets from 'a' to 'z' to our $alpha
    for ($i = 65; $i <= 122; $i++) {
        $alpha .= chr($i);
    }
    return $alpha;
}

function gen_code($len = 1)
{
    $alpha = gen_code_alpha();

    $strlen = strlen($alpha);

    $code = '';
    for ($k = 0; $k < $len; $k++) {
        $i = rand(0, $strlen -1);// now wanna randomly generate the code
        $code .= substr($alpha, $i, 1);
    }

    return $code;
}

function gen_license_key()
{
    global $licenseKey;
    $licenseKey = gen_code(4) . '-' .
                  gen_code(4) . '-' .
                  gen_code(4) . '-' .
                  gen_code(4) . '-' .
                  gen_code(2);
}

gen_license_key();
echo $licenseKey;
?>

在线尝试!(演示)

于 2018-08-06T18:18:25.447 回答
0
<?php

function gen_code_alpha()
{
    $alpha = '';

    for ($i = 0; $i <= 9; $i++) {
        $alpha .= $i;
    }

    // This attaches alphabets from 'a' to 'z' to our $alpha
    for ($i = 65; $i <= 122; $i++) {
        $alpha .= chr($i);
    }
}

function gen_code($len = 1)
{
    gen_code_alpha();
    global $alpha;

    $strlen = strlen($alpha);

    $code = '';
    for ($k = 0; $k < $len; $k++) {
        $i = rand(0, $strlen -1);// now wanna randomly generate the code
        $code .= substr($alpha, $i, 1);
    }

    return $code;
}

function gen_license_key()
{
    $licenseKey = gen_code(4) . '-' .
                    gen_code(4) . '-' .
                    gen_code(4) . '-' .
                    gen_code(4) . '-' .
                    gen_code(2);
return $licenseKey;
}

$licenseKey = gen_license_key();
echo $licenseKey;
?>

我们需要在函数中返回一个变量,而且$rand不是变量rand()是函数。

于 2018-08-06T18:20:28.820 回答