随机数的生成
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/3/28 * Time: 9:00 */ //print_r(range(0,9)); class RandString{ //这个属性表示我们随机数的长度 private $length; //type:1:表示数字类型的随机数;2:字母的随机数,3:数字和字母的随机数 private $type; public function __construct($length,$type){ $this->length=$length; $this->type=$type; } public function randNum(){ switch($this->type){ case 1: //join返回由数组组合成的字符串,是implode函数的别名 return join(array_rand(range(0,9),$this->length)); break; case 2: //array_merge 合并一个或多个数组,array_flip交换数组中的键和值 return join(array_rand(array_flip(array_merge(range('a','z'),range('A','Z'))),$this->length)); break; case 3: return join(array_rand(array_flip(array_merge(range(0,9),range('a','z'),range('A','Z'))),$this->length)); break; } } } $strNum1=new RandString(4,1); echo $strNum1->randNum(); echo "-------"; $strNum2=new RandString(4,2); echo $strNum2->randNum(); echo "-------"; $strNum3=new RandString(4,3); echo $strNum3->randNum(); //print_r(array_rand(4));
效果: