Php random string or number tutorial

There are some occasions, when we need a random string. For example when we want to generate password or we want to send verification code or captcha code etc. For generating random string with php, we shall make a function getRandom(), you may use any name. I have set default string length 10.In this, php rand() function will help to generate a string


 function getRandom($len=10){
		$string="0123456789abcdefghijklemnopqrstuvwzyxABCDEFGHIJKLMONOPQRSTUVWXYZ";
        $random='';
        for($i=0;$i<$len;$i++){
        $random.=$string[rand(0,61)]; }
        return $random;
       }
If you see here we took 0 to 9 digits, English small letters and English capital letters. Because here, we want random string with mixed characters. You may take only digits or English small/capital letters as per your requirement. In rand function we passed two arguments, first one is minimum integer and second one is maximum integer. Our minimum value is 0 and maximum value is 61, because we have total 62 characters. And here $string variable is used as an array.