Below is the code which im using to generate random string , is there any chance that i might get duplicate
<?php
echo random_int(100,999).substr(uniqid(),-4);
?>
Below is the code which im using to generate random string , is there any chance that i might get duplicate
<?php
echo random_int(100,999).substr(uniqid(),-4);
?>
Yes, in fact there is a relatively likely chance that you will get a duplicate. Consider the following test:
$arr = [];
$i = 0;
while (1) {
echo ++$i . PHP_EOL;
$val = random_int(100,999).substr(uniqid(),-4);
if (in_array($val, $arr)) break;
$arr[] = $val;
}
Took me less than 5000 iterations on the first try to hit a duplicate. The only way to 100% guarantee that a string is unique is to compare it against the others in a set and try again if it matches one of them. If all you need is unique - just use an autoincrement.