Hello friends hope you all are doing good and please be safe in this PANDEMIC situation. Wear Mask always. Generating random alphanumeric value in PHP can be done using some methods. All the methods have their own working style and output. Do you know generating random values can help you to make OTP and Password’s
- rand($min, $max)
- mt_rand($min, $max)
- random_int($min, $max)
- rand_chars($length)
- generate_random_string($length)
- random_crypt_value($length)
rand($min,$max) – This function will allow us to get the value lie between minimum and maximum numbers. Previously rand() was bit slow but after the PHP 7 speed has been increased and now it works fast. rand() will help you to generate the numeric value between the minimum and maximum like rand(999,9999) will give you minimum value of 999 and max as 9999 or anything which lies in both the values. rand() is an int based function so you have to insert only integer value. You can also use it without giving any min or max number to the function.
Uses : – echo rand(5, 15); It will return anything like 9 or 10 etc.
mt_rand($min, $max) – This function will allow us to get the value lie between minimum and maximum numbers. Generate a random value via the Mersenne Twister Random Number Generator. mt_rand() will produce random numbers four times faster than what the average mt_rand() provides. mt_rand() will help you to generate the numeric value between the minimum and maximum like rand(0,999999) will give you minimum value of 0 and max as 999999 or anything which lies in both the values. But it will return the values 4 times faster then the rand().
Uses : – echo mt_rand(5, 15); It will return anything like 9 or 10 etc.
random_int($min, $max) – This function will allow us to generates cryptographically secure pseudo-random integers. It will generate random numbers between the provided $min and $max values, which default to PHP_INT_MIN and PHP_INT_MAX. Unfortunately, this function is only available starting from PHP 7.0.
rand_chars($length) – This function is not the core PHP function. This is created by me so you have to use the whole function for using this. This function will take the length as parameter so that it will create the random alpha of same length. It will choose the random value from 65 to 90 then change them to character using chr(). Function generate random alphabetic string.
function rand_chars($length){ for($i=1;$i <=$length;$i++){ $random_string.=chr(rand(65,90)); } return $random_string; } echo rand_chars(8); ?>
Uses :- echo rand_chars(8); It will return MBHJUIAS etc.
generate_random_string($length) – This function is not the core PHP function. It is created by me so you have to use the whole function for using this. This function will take the length as parameter so that it will create the random alpha numeric value of same length. This function generate random alphanumeric string.
function generate_random_string($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[mt_rand(0, $charactersLength - 1)]; } return $randomString; } echo generate_random_string(8);
Uses :- echo generate_random_string(8); It will return AE1T8OM6 etc.
random_crypt_value($length) – This function is not the core PHP function.This is created by me so you have to use the whole function for using this. This function will return an array with normal number and same number in encrypted format. $length will be int it will decide the length of the value.
function randomCryptValue($length){ $digits = $length; $randValue=rand(pow(10, $digits-1), pow(10, $digits)-1); $normalrandValue= $randValue; $newrandValue= $normalrandValue; $encryptrandValue=$newrandValue; $randVal = urlencode($encryptrandValue); $randVal_crypt = crypt($randVal); $randVal_array =array("1"=>$normalrandValue,"2"=>$randVal_crypt); return $randVal_array; } print_r(randomCryptValue(8));
Uses :- print_r(randomCryptValue(8)); It will return Array ( [1] => 19310775 [2] => $1$MtJ2rxll$kmqgwnXA9yZ01HkP05TVZ1 ) etc.
Hope you like the tutorial and our functions to generating random values as per need. If you have any advice please let me know comment below. Please share it with others.
~ Stay Home Be Safe Use Mask ~
No Comments