Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP实现活动人选抽奖功能的方法

PHP实现活动人选抽奖功能的方法

墨辰丷
墨辰丷asal
2018-05-23 17:12:122146semak imbas

这篇文章主要介绍了PHP实现活动人选抽奖功能,随机抽取指定人数,依次列举被抽中的人名,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文介绍情景为活动人选抽奖,通过简单随机抽取指定人数,依次列举被抽中的人名。示例代码如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>活动人选</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].&#39;<br/>&#39;;
  }

  // echo &#39;抽中人:&#39;. $lotterPeople;



  /**
   * 生成随机数
   * $type int 默认(字母&&数字) 1 纯字母 2.纯数字 3.英文单词
   */
   function GetRandStr($len=&#39;4&#39;,$type=&#39;3&#39;)
  {


    switch($type)
    {
      case &#39;1&#39;;
      $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 &#39;2&#39;;
      $chars = array(
          "0", "1", "2",
          "3", "4", "5", "6", "7", "8", "9"
      );
      break;
      case &#39;3&#39;;
      $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>

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php自动加载方式集合_php技巧

PHP函数shuffle()取数组若干个随机元素的方法分析_php技巧

PHP使用stream_context_create()模拟POST/GET请求的方法_php技巧

Atas ialah kandungan terperinci PHP实现活动人选抽奖功能的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn