php抽奖小程序的实现代码

yipeiwu_com6年前PHP代码库

这个抽奖小程序,在实际的测试环境中也可以用到,比方说测试数据的查询在in条件下,要查询随机的5个id,然后在用ab去压测

复制代码 代码如下:

<?php
 /**
  * “抽奖”函数
  *
  * @param integer $first    起始编号
  * @param integer $last     结束编号
  * @param integer $total    获奖人数
  *
  * @return string
  *
 */
 function isWinner($first, $last, $total)
 {
     $winner = array();
     for ($i=0;;$i++)
     {
         $number = mt_rand($first, $last);
         if (!in_array($number, $winner))
             $winner[] = $number;    // 如果数组中没有该数,将其加入到数组
         if (count($winner) == $total)   break;
     }
     return implode(' ', $winner);
 }
 // for test
 echo isWinner(1, 100, 5);
 ?>

相关文章

php inc文件使用的风险和注意事项

数据库使用中需要关注的主要问题之一是访问权限即用户名及密码的暴露。在编程中为了方便,一般都会用一个db.inc文件保存,如:复制代码 代码如下:<?php $db_use...

php中iconv函数使用方法

iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。 1、下载libiconv函数库http://ftp.gnu.org/pub/gnu/libiconv/l...

用php实现的获取网页中的图片并保存到本地的代码

复制代码 代码如下:<?php header("Content-type:image/jpeg"); function read_url($str) { $file=fopen($...

PHP删除特定数组内容并且重建数组索引的方法.

复制代码 代码如下: $a = array('a','b','c','d'); unset($a[2]); print_r($a); 但是这种方法的最大缺点是没有重建数组索引. 经过查资...

php中错误处理操作实例分析

php中错误处理操作实例分析

本文实例讲述了php中错误处理操作。分享给大家供大家参考,具体如下: 错误触发 有2种触发: 系统触发: E_NOTICE:提示性错误,比如使用不存在的变量或常量 E_WARNING:警...