php多个随机比率计算
更新:HHH   时间:2023-1-7


<?php

header("Content-type:text/html;charset=utf-8"); 

?>

<form method="post" action="">

<input type="text" name="bl"/><input type="submit" value="计算"/>

<p> 多个随机比率英文,分割 例:50%,5%,0.5% </p>

</form>

<?php

if(!empty($_POST)){

echo $_POST['bl'];//获取值

echo "<hr/>";

$yarr = $arr = explode(',',$_POST['bl']);//字符串转为数组

print_r($arr);

echo "<hr/>";

rsort($arr);//数组按值重大到小排序

print_r($arr);

echo "<hr/>";

echo end($arr);//取最小值

echo "<hr/>";

$bl = end($arr);//取最小值赋值

echo trim($bl,'%');

echo "<hr/>";

echo strstr(trim($bl,'%'),'.');//去除%,并根据小数点截取

if(strstr(trim($bl,'%'),'.')){//去除%,并根据小数点截取,判断是否截取到值

    $cishu = strlen(strstr(trim($bl,'%'),'.'))-1;

    $beishu = 10;

    for($i=1;$i<$cishu;$i++){//小数点前移

        $beishu = $beishu*10;

    }

}else{

    $beishu = 1;

}

echo $beishu;

echo "<hr/>";

$count = '';

$nums = '';

foreach($yarr as $val){

$count += $num[] = $nums = $val*$beishu+$nums;//取值范围重新赋值

}

echo $count;

echo "<hr/>";

print_r($num);

echo "<hr/>";

$rand_num = rand(1,$beishu*100);//根据倍数换算,随机取比率范围内的值

$k = "";

foreach($num as $key=>$val){

if($key == 0){//判断是否是第一次循环

if($rand_num >= 1 && $rand_num <= $val){//按范围判断是否出现在该中奖段

echo "恭喜你中奖了";

$k = $key;

break;

}

}else{

if($rand_num > $num[$key-1] && $rand_num <= $val){//取上一次的值和本次比较,判断是否出现在该中奖段

echo "恭喜你中奖了";

$k = $key;

break;

}

}

}

echo $k;

}

?>







返回web开发教程...