任务分解:
1、生成6个无序的1-33之间的随机数作为红球
2、生成一个1-16之间的随机数作为蓝球
3、设置开奖按钮,刷新页面。
<?php
/**
* 双色球开奖功能实现
*/
header('Content-Type:text/html; charset=UTF-8');
//1、随机生成6个红球,数字在1-33之间。
$red = range(1,33); //range()生成一个1-33之间的随机数,两个参数为区间的上下线
$tmp = array_rand($red,6); // array_rand()参数随机取出6个数字,第一个参数是数字的来源,第二个参数是个数
shuffle($tmp); //打乱顺序
//为了让数字看起来格式统一,在个位数的数字前面添加前导0
foreach ($tmp as $i){
$red_ball[] = $red[$i]<10 ?('0'.$red[$i]):$red[$i];
}
//2、随机生成1个蓝球,数字在1-16之间
$blue = mt_rand(1,16);
//给蓝球添加前导0
$blue_ball = $blue < 10 ?('0'.$blue):$blue;
//输出
echo '<div style=" background-color: lightsteelblue; margin: 30px auto; width: 40%;height: 100px;text-align: center;padding-top: 20px">';
foreach ($red_ball as $value){ //将红球循环取出放入变量$value中
echo "<span style='color: red;font-size: 24px; font-weight: bolder;'>$value </span>";
}
echo "<span style='color: blue;font-size: 24px;font-weight: bolder;'>$blue_ball</span><br><br>";
echo '<button onclick="window.location.reload()" style="width: 40%">开奖</button>';
echo '</div>';
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号