0

I was trying to create a program in which simply generates license keys. Here it is:

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

But when I try to run it, it doesn't show any outputs. I'm new to PHP and I don't know alot about debugging, But I had some tries. I put some echos in the code which I understood my $alpha is being successfully generated in gen_code_alpha() function. I also tried to echo $licenseKey in gen_license_key() function which didn't help. I didn't find out anything more.

So what do you think?

amirali
  • 1,888
  • 1
  • 11
  • 32

5 Answers5

2

You are almost there, just change this:

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

And then you can:

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

Additionally, set global $alpha = ''; inside your gen_code_alpha() function or I highly recommend following a "return" pattern for programming functions.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • Wouldn't `gen_license_key` still need a return value in your second solution? – No Name Aug 06 '18 at 18:06
  • This solution is great, because you want to try to avoid polluting the [global namespace](http://php.net/manual/en/language.namespaces.global.php). – Sunny Patel Aug 06 '18 at 18:06
  • @BojanSrbinoski sorry for the confusion. Changing the function to use `return` is required for both output methods. See my update. – MonkeyZeus Aug 06 '18 at 18:07
  • @MonkeyZeus that makes more sense, but his `gen_code_alpha()` function doesn't have a return value so this still won't work. EDIT: never mind, I just noticed the `global` keyword – No Name Aug 06 '18 at 18:11
  • 1
    @Amirali Use my answer as a guide to fix your other functions. Avoid global variables at all costs because they are a cancer to PHP. This isn't code review; if you made the mistake once then verify the rest of your code isn't causing the same issue. I'm not your mother. – MonkeyZeus Aug 06 '18 at 18:12
  • @BojanSrbinoski See my update again. Globals should be avoided at all cost in modern PHP... – MonkeyZeus Aug 06 '18 at 18:14
  • @MonkeyZeus I returned $alpha in gen_code_alpha() and $code in gen_code(), also added $alpha = gen_code_alpha(); , And did all other solutions, And Again it didn't work!!! – amirali Aug 06 '18 at 18:19
0

Variables defined inside functions exist only inside those functions. Instead of setting a global variable, you should return the value from your function:

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

Then you can get a new value by assigning a variable to the value returned by your function:

$licenseKey = gen_license_key();
echo $licenseKey;
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
0

Here is the complete code working

Notice that you have a typo (rand, not $rand)

I also would suggest that you change the code to avoid using global variables, which is a bad practice.

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

?>

outputs:

 l:3X6D-gnQL-I0zJ-TRQD-Sq
justadev
  • 1,168
  • 1
  • 17
  • 32
0

If you really want to go down this path of properly using global variables, then you would have to reference them first, and early if you are assigning to them. Also, since it seems like you are not utilizing $alpha outside of those functions, just return those values and avoid that global namespace pollution.

You also have a typo where $rand isn't the name of a variable, but the function rand(), which has been corrected.

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

Try it Online! (Demo)

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
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;
?>

We need to return a variable in function and also $rand is not a variable rand() is a function.

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
  • 1
    Please explain the differences in your code. You provide a code-only answer, which is discouraging since it doesn't help others understand your thought process or reasoning on why this would be a good answer to the question. – Sunny Patel Aug 06 '18 at 18:24
  • Kindly check last line i explain the error also in comment – Mahalakshmi Aug 06 '18 at 19:17
  • I did not scroll down to see the text initially, because I assumed everything in the code block was code. I fixed your answer to reflect this more accurately. – Sunny Patel Aug 06 '18 at 19:24
  • 1
    I am new to stack overflow, i will try to improve myself to add the comments more understandable. – Mahalakshmi Aug 06 '18 at 19:28