PHP random string generator

PHP random number function, PHP random string

0 Comments

PHP Random String Function

<?php
function random_string($length = 10) {
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_';
    $characters_length = strlen($characters);
    $string = '';
    for ($i = 0; $i < $length; $i++) {
        $string .= $characters[rand(0, $characters_length - 1)];
    }
    return $string;
}

//usage
echo random_string(15); //d8iSfUi8484_rij

?>

PHP Random Number Function

<?php
function random_number($length = 10) {
    $numbers = '0123456789';
    $numbers_length = strlen($numbers);
    $number = '';
    for ($i = 0; $i < $length; $i++) {
        $number .= $numbers[rand(0, $numbers_length - 1)];
    }
    return $number;
}

//usage
echo random_number(15); //659854320686554
?>
0 comments
Leave a Reply