Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
generate random numbers in php with no repeats
function randomDigits($length){
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}
@saiedlakhdar

This comment has been minimized.

Copy link

@saiedlakhdar saiedlakhdar commented Jan 3, 2018

i got error undefined $digits variable ^^

@abarghaz

This comment has been minimized.

Copy link

@abarghaz abarghaz commented Mar 15, 2018

Hi

you should to give a value to variable before loop

Like this :

function randomDigits($length){
$numbers = range(0,9);
shuffle($numbers);
$digits = "";
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}

@nanadjei

This comment has been minimized.

Copy link

@nanadjei nanadjei commented Jun 12, 2018

Getting the same error.

@arif98741

This comment has been minimized.

Copy link

@arif98741 arif98741 commented Jun 14, 2018

Problem solved

$digits = '';
function randomDigits($length){
    $numbers = range(0,9);
    shuffle($numbers);
    for($i = 0; $i < $length; $i++){
    	global $digits;
       	$digits .= $numbers[$i];
    }
    return $digits;
}

`

@iconmedia

This comment has been minimized.

Copy link

@iconmedia iconmedia commented Feb 26, 2019

How can i authenticated this against database values?

@trelljaret

This comment has been minimized.

Copy link

@trelljaret trelljaret commented Apr 20, 2020

BS

@sodmond

This comment has been minimized.

Copy link

@sodmond sodmond commented Aug 26, 2020

How can i authenticated this against database values?

You can just search the value in your database, if it returns null then it has not been stored yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.