Generate random numbers for OTP or captcha code in php

0
generate random number in php using rand and str_shuffle

Hello, Have you ever thought to generate random numbers in PHP well today I will tell you that how you can generate random numbers for OTP or captcha-code in PHP language? Well, it's that much hard as you are thinking I will tell you the simplest way to generate random numbers.

We will generate random numbers using the rand and str_shuffle function.

Generate 4 digits random number using rand function

rand(); function is a pre-defined PHP function which is used to generate rand number as given parameter the below example uses to generate 4 digits random number between 1000 to 9999.

<?php

$shiv = rand(1000, 9999);
echo $shiv;

?>

We can also generate random more than 4 digits instead of using 1000 we can also use 100000 to 999999 for generating 6 digits random number using the rand function

<?php

$shiv = rand(100000, 999999);
echo $shiv;

?>

Let's generate random numbers using str_shuffle

the above example used to generate random numbers in numeric value now we will generate number alpha number random numbers. This function will help users to choose the character parameter.

<?php

$shiv = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$now = str_shuffle($shiv);
echo substr($now, 0,4);

?>

$now variable we have generated random characters using given parameters but it generates 36 digits long but we need to generate 4 digits or 6 digits random number for that we are using substr(); function which helps users to cut the string's part which we want, in the function 0 means the starting point of the string and 4 means the total number of extracted string which we want to show. If we want to generate 6 digits random number we can type 6 instead of 4 in the above function.

Generate random number using str_shuffle repeat numbers

In the above str_shuffle we were getting none repeated numbers there was all the digits unique value now we will generate random numbers which can be repeated using str_shuffle.

<?php

$shiv = substr(str_shuffle(str_repeat("ABCDEFGIJKLMNOPQRSTUVWXYZ0123456789" 5)), 0,4);
echo $shiv;

?>

This function helps us to generate 4 digits random numbers that can be repeated. This function is the same as the above function but here we have used str_repeate(); function which helps us to repeat the strings.

Let's see one more way to generate a random number using the str_shuffle function

<?php

$char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$pass = substr(str_shuffle($char), 0,6);
echo $pass;

?>

I hope now you can generate random number easily. I have used Capital Alphabet letters you can use small alphabets and capital alphabets letters both in a string as you want.

Watch video on generating random numbers in PHP

This video is more helpful for you I have covered all the above function in this video you must watch and share with your friends so they can all get to know this information.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !