相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡(jiǎn)單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說(shuō)明
- thinkPHP5框架實(shí)現(xiàn)分頁(yè)查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
PHP實(shí)現(xiàn)活動(dòng)人選抽獎(jiǎng)功能
本文介紹情景為活動(dòng)人選抽獎(jiǎng),通過(guò)簡(jiǎn)單隨機(jī)抽取指定人數(shù),依次列舉被抽中的人名。示例代碼如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>活動(dòng)人選</title> </head> <body> <?php $lotterPeopleArr = array(); $num = 8; for($i=0;$i<$num;$i++){ $lotterPeople = GetRandStr(1); while(in_array($lotterPeople, $lotterPeopleArr)){ $lotterPeople = GetRandStr(1); } $lotterPeopleArr[$i] = $lotterPeople; echo "抽中人:". $lotterPeopleArr[$i].'<br/>'; } // echo '抽中人:'. $lotterPeople; /** * 生成隨機(jī)數(shù) * $type int 默認(rèn)(字母&&數(shù)字) 1 純字母 2.純數(shù)字 3.英文單詞 */ function GetRandStr($len='4',$type='3') { switch($type) { case '1'; $chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ); break; case '2'; $chars = array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ); break; case '3'; $chars = array( "小華","小冠","小王","小吳","小朱","小何","小陳","小熊","小斌","小李","小春","小超","小張","小郭","小柯","小梁","小理","小龔","小杰","小余","小洪","小鄭","小志","小潘", ); break; default; $chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ); break; } $charsLen = count($chars) - 1; shuffle($chars); $output = ""; for ($i=0; $i<$len; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } ?> </body> </html>