Generate Random Strings And Numbers in PHP
How to create a random string of PHP. The code below for creating a unique ID number function.
PHP Random String Function
This PHP random_strings() function creates a combination of uppercase and lowercase letters, including underscore.
<?php
function random_strings($length = 10) {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_';
$chars_length = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $chars[rand(0, $chars_length - 1)];
}
return $string;
}
//usage. set strings length
echo random_strings(15); //output d8iSfUi8484_rij
?>
PHP Random Number Function
This PHP random_numbers() function generates mixed numbers.