How to use random in php

In php, we can use the random function

1. how to generate unique random number in php

$numbers = range (1, 20);
shuffle ($numbers);

Or use the function

function randomNumner ($ min, $ max, $ quantity) {
  $numbers = range ($min, $max);
  shuffle ($numbers);
  return array_slice ($numbers, 0, $quantity);
}

2. How to generate 5 digit numbers?

randomNumner ($ min, $ max, 5);

3. php generate random alphanumeric

If you want to generate random alphanumeric strings from a fixed set of characters, you can use the str_shuffle ($string) function. This function returns you a random shuffled string.

$allowed_chars = '0123456789abcdefghijklmnopqrstuvwxyz';
// Output: 54esmdr0qf
echo substr (str_shuffle ($permitted_chars), 0, 10);

 

Add a Comment

Your email address will not be published. Required fields are marked *