rand in php – Generate a random number in PHP

rand in php : Using the PHP Rand() Functions to Generate Random Numbers. also The rand() function is used in PHP to generate a random integer. Use of random_int(), Use of rand() function without argument, Use of rand() function with arguments, Php Random Float and Use of rand () function with bitwise operator.

The basic syntax of rand() function is following: rand in php

rand();  
or  
rand(min,max); 
rand in php
rand in php

How to Generate random numbers by PHP RAND function?

Generate random numbers:

");
echo(rand() . "
"); echo(rand(1,50)); ?>

rand in php

Parameter Description Required/Optional
min Specifies the lowest number to returned. Optional
max Specifies the highest number to be returned. Optional

The example of rand without parameters


Don’t Miss : PHP Strong Random Number And String Generator

generate 2 digit unique random number in php

echo(rand(10,99) . "
");

generate 4 digit unique random number in php

echo(rand(1000,9999) . "
");

generate 4 digit unique random number in php

echo(rand(100000,999999) . "
");

generate 8 digit unique random number in php

echo(rand(10000000,99999999) . "
");

Different uses of rand () function

 first : ". rand(). "

"; echo "

second : ". rand(). "

"; echo "

range [10-100]: ". rand(10,100). "

"; echo "

range [100-500]: ". rand(100,500). "

"; echo "using bitwise operator (&) with 10: "; echo rand()&10 ; echo "
using bitwise operator (&) with 50: "; echo rand()&50 ; ?>

rand () Function in PHP

Let’s see how to generates a 4 digit number.


Random number of different length

Random Value = $var"; 
?>

I hope you get an idea about rand in php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment