random password generator php code example

Example 1: auto generate password in php

function rand_string( $length ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return substr(str_shuffle($chars),0,$length);
}

echo rand_string(8);

Example 2: random number generator in php

you can use rand() function for that in php.
Example:
Generate random numbers between 1 to 50
<?php
  echo rand(1,50);
?>

Example 3: online password generator in php

<?php
function randomPassword() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

echo randomPassword();
?>

Example 4: php random number generator

<?php
  echo rand(1,50);
?>

Tags:

Php Example