我尝试用php代码生成随机数(3个数字),但是当我尝试返回值时,它不起作用 [英] I try to generate random (3 numbers) numbers with the php code, but when I try to return the value it doesn't work

查看:119
本文介绍了我尝试用php代码生成随机数(3个数字),但是当我尝试返回值时,它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的php脚本:
当我尝试将值返回到$ total并且之后返回函数generate_random_3时,在某些情况下它不会生成任何数字,请帮助

Here is the php script of mine: When I try to return value to $total and echo function generate_random_3 afterwards, in some cases it doesn't generate any number, please help

<?php
    function generate_random_3(){

        for($x=0; $x<3; $x++){
            $random[] = rand(1, 9);
        }
        list($first, $second, $third) = $random;

        if($first!=$second AND $first!=$third AND $second!=$third AND $first!=0){
            $total = $first.$second.$third;
            return $total;
            }else{
            generate_random_3();
            }
    }
    echo generate_random_3();
?>


推荐答案

function generate_random_3() {
  return rand(100,999);
}

echo generate_random_3();

这篇关于我尝试用php代码生成随机数(3个数字),但是当我尝试返回值时,它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆