Skip navigation.

Contridentuallity

Not a fact, a theory!?

PHP Random Digit

, , ,

Just a quick snippet which will help you to create a random number in a specific range, using PHP. PHP has a built in rand(); fuction which allows you to do this with ease. See the example below :

PHP Random Function :
rand([int_min], [int_max]);


Example :
rand(1, 999);


The code shown above will create a random number between 1 (which is the minimum) and 999 (which is the maximum).

You can make the [min] and [max] values of this function dynamic, simply by replacing the digits with your variables. You can also assign this function to a variable itself and ouput the results if needed. See the simple example below :

$min = 1;
$max = 99;

$random_number = rand($min, $max);

echo "$random_number";


I wouldn't say that this function is really used that often, but it definitely comes in handy from time to time. Enjoy! :wink:

Browser StatsMySQL Last Inserted ID

Comments

satya61229 5. September 2009, 04:09

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.

Download Opera, the fastest and most secure browser
November 2009
S M T W T F S
October 2009December 2009
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30