<?phpclass functions{//随机生成0.1到10的随机数public static function getNumber(){$arr = ['0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1','1.1','1.2','1.3','1.4','1.5','1.6','1.7','1.8','1.9','2','2.1','2.2','2.3','2.4','2.5','2.6','2.7','2.8','2.9','3','3.1','3.2','3.3','3.4','3.5','3.6','3.7','3.8','3.9','4','4.1','4.2','4.3','4.4','4.5','4.6','4.7','4.8','4.9','5','5.1','5.2','5.3','5.4','5.5','5.6','5.7','5.8','5.9','6','6.1','6.2','6.3','6.4','6.5','6.6','6.7','6.8','6.9','7','7.1','7.2','7.3','7.4','7.5','7.6','7.7','7.8','7.9','8','8.1','8.2','8.3','8.4','8.5','8.6','8.7','8.8','8.9','9','9.1','9.2','9.3','9.4','9.5','9.6','9.7','9.8','9.9','10'];$rand_keys = array_rand($arr, 1);$number = $arr[$rand_keys];return $number;}}print_r(functions::getNumber());exit();
结果如下:
<?php/** 方法类* */class functions{/*** PHP随机生成验证码函数** @param array* @return mixed*/function randCode($params = []){$num = $params['num'] ?? 4; //验证码个数$isLetter = $params['isLetter'] ? $params['isLetter'] : 1; //1是纯数字 2是字母和数字的组合if($isLetter==1){for($i=1;$i<=$num;$i++){$codeMin.=0;$codeMax.=9;}return rand($codeMin,$codeMax);}if($isLetter==2){//如果想调整权重,自己可以根据需求修改$codeArr这个一位数组$codeArr = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','e','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'];//随机返回数组中的键名$codeKeys = array_rand($codeArr,$num);//把数组中的元素按随机顺序重新排列。shuffle($codeKeys);foreach ($codeKeys as $codeValue){$codeStr .= $codeArr[$codeValue];}return $codeStr;}}}//测试生成验证码方法$re = (new functions())->randCode(['num'=> 6, //需要的个数'isLetter' => 2, //1是纯数字 2是数字加字符串]);print_r($re);
结果如下: