The following function will create a random string output what is the best way to prevent duplicate outputs / collisions if called multiple times.
function random_string($length) {
$key = '';
$keys = array_merge(range('A', 'Z'), range('a', 'z'), array('_'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
echo "First : " . random_string(rand(3, 50));
//These have a small percentage of a chance of matching the previous random output lets eliminate all possibility of getting the same output.
echo "Second : " . random_string(rand(3, 50));
echo "Third : " . random_string(rand(3, 50));
echo "Fourth : " . random_string(rand(3, 50));
I did read on the PHP documentation that array_unique could achieve what i want but would it be the best solution or is there a more efficient way.
Array_uniquewill eliminate not unique elements, so instead of 4 strings you will get three or two or even one.