Contridentuallity

Not a fact, a theory!?

PHP Random string

, , , ,

Generate a random string with Php

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

Update: You can use PHP uniqid() function for the same purpose. It is a built-in function and easy to use.

Browser StatsMySQL Last Inserted ID

Comments

Satya Prakashsatya61229 Saturday, September 5, 2009 4:09:30 AM

Andre Nedved435andrei6 Monday, November 22, 2010 5:13:53 PM

thanks

Write a comment

New comments have been disabled for this post.

May 2012
S M T W T F S
April 2012June 2012
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 31