Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我尝试用php代码生成随机数(3个数字),但当我尝试返回值时,它不会';行不通_Php_Function_Recursion_Return - Fatal编程技术网

我尝试用php代码生成随机数(3个数字),但当我尝试返回值时,它不会';行不通

我尝试用php代码生成随机数(3个数字),但当我尝试返回值时,它不会';行不通,php,function,recursion,return,Php,Function,Recursion,Return,以下是我的php脚本: 当我尝试将值返回到$total并在之后生成echo函数\u random\u 3时,在某些情况下它不会生成任何数字,请帮助 <?php function generate_random_3(){ for($x=0; $x<3; $x++){ $random[] = rand(1, 9); } list($first, $second, $third) = $random;

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

<?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();
?>

无论哪种方式

$number1 = rand(0,365);
$number2 = rand(0,365);
$number3 = rand(0,365);


echo $number1 . " " . $number2 . " " . $number3;

请显示相关的代码段、它应该返回的内容以及它实际返回的内容。在else中重复调用
generate\u random\u 3()
之前,您错过了一个
return
。它会返回一些内容,但被调用函数需要返回该值,最好使用mt_rand()而不是rand()
function generate_random_3() {
  return rand(100,999);
}

echo generate_random_3();