Examples of Random Functions In PHP

  No comments
The random function is used to randomize a character, to create a random with Integer / Figures type can use the default function of PHP that is with RAND (min, max) as an example:

    <?php
    echo rand (1.10);
    ?>

Then its output is 1-10

But how to make random with letters / alphabets?
The author has not found any special function for random alphabet / letters from PHP, but There are many ways to random functions with these letters / alphabets, including:
Example 1:

    function random ($long) {
       $character = 'ABCDEFGHIJKLMNOPQRSTUabcdefghijklmnopqrstuvwxyz1234567890';
       $String = '';
       For ($i = 0; $i <$long; $i ++) {
           $pos = rand (0, strlen ($character) -1);
           $string. = $Character {$mail};
       }
       return $string;
    }
    echo random (11); // The output of numbers and random letters is 11 characters


 Example 2:

    <?php
    $a1 = str_repeat ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5);
    $a2 = str_shuffle ($a1);
    echo substr ($a2,0,10); // The output is random numbers and letters of 10 characters
    ?>

 Example 3:

    for ($i = 1; $i <= 10; $i ++) echo chr (rand (97,122));

No comments :

Post a Comment