Random number, string generation in PHP
Explaining/Illustrating different ways to generate random numbers and strings in PHP
<?php /********************************* <span id="more-43"></span> RANDOM STRING GENERATION Explaining/Illustrating different ways to generate random numbers and strings Programmed By: Mukesh Chapagain http://www.chapagain.com.np http://blog.chapagain.com.np *********************************/ //generating random numbers //generating random numbers between 1 and 1000 $num = rand(1,1000); echo "Random number from rand function: "; echo $num; echo "<br/><br/>"; //we can use mtrand() function for better random number generation $mt_num = mt_rand(1,1000); echo "Random number from mt_rand function: "; echo $mt_num; echo "<br/><br/>"; //generating a unique combination of numbers and letters //works with PHP5 or higher $unique = uniqid(); echo "Random string from uniqid function: "; echo $unique; echo "<br/><br/>"; //making the string more complex generated by uniqid function //by using md5() function //md5 function generates 32 character long string //the function uniqid() works only with PHP5 or higher $md5_unique = md5(uniqid()); echo "Random string from md5 to uniqid function: "; echo $md5_unique; echo "<br/><br/>"; //generating random string from a given string //randomly shuffling a given string $string = "Mukesh Chapagain"; $rand_string = str_shuffle($string); echo "Random string from str_shuffle function: "; echo $rand_string; echo "<br/><br/>"; //generating random number from time() function //the number changes every second //this returns the current unix timestamp $time = time(); echo "Random number from time function: "; echo $time; echo "<br/><br/>"; ?>
Related posts:
- PHP: Generating Multiple Random String
- Generating random image
- PHP: How to get integer or decimal from a string?
- PHP: Parse Unparse String Array
- Magento: Set Random Order in Collection using RAND()
- PHP: Simple and easy way to format URL string
- jQuery: How to replace string, div content and image src?
- PHP : Read Write Xml with SimpleXML
- Javascript: How to get current URL without Query string?
- Convert string to datetime and datetime to string in C#
