I have a string with a lot of different numbers. I am trying to create a new random number and add it to the string.
The part I need help with is "if the number already exists in the string, create a new random number, and keep doing it until a number is created that does not yet exist in the string".
// $string contains all the numbers separated by a comma
$random = rand(5, 15);
$existing = strpos($string, $random);
if ($existing !== false) { $random = rand(5, 15); }
$new_string = $string.",".$random;
I know this isn't quite right as it will only check if it's existing once. I need it to keep checking to make sure the random number does not exist in the string. Do I use a while loop? How would I change this to work properly?
Your help is much appreciated.