douxianliu6756 2013-12-15 13:29
浏览 26
已采纳

如何在函数本身中输出函数的参数

<?php


$l = 4000;

function flip($h, $t)
{

    if ($h + $t <= 29) {
        $a = mt_rand(0, 1);
        if ($a == 1) {
            flip($h + 1, $t);
        } else {
            flip($h, $t + 1);
        }

    } else {

        echo "$h,$t" . "</br>";
    }

}

$ar = array( "test");


for ($i = 0; $i <= $l; $i++) {
    array_push($ar, flip(0, 0));
}


?>

My code is supposed to give the number of tails and heads upon spinnings 30 times. And this process is done 4000 times. I want to store all the results of the 4000 times in an array using the above code. But I think the way I defined by flip is wrong. I didn't know how to store the value of the arguments of flip after it is done therefore I used an echo.

When i run this code.. for some reason it is printing out all the answers like this : http://searchr.us/testing/ip.php instead of just making an array. I have pin pointed that the way I took the vfilanl values of flip wrong. Can someone tell me the difference between echo , print and how DO i use return. When i just use return(); after echo.It gives me an error.

Please clarify my doubt on how I can output these values.

  • 写回答

1条回答 默认 最新

  • duan01203 2013-12-15 13:39
    关注

    You should use something like this:

    <?php
    
    function flip($h, $t)
    {
    
        if ($h + $t > 29) return $h . ',' . $t;
        $a = mt_rand(0, 1);
        return $a == 1 ? flip($h + 1, $t) : flip($h, $t + 1);
    
    }
    
    $ar = array( "test");
    
    $l = 4000;
    for ($i = 0; $i <= $l; ++$i) {
        array_push($ar, flip(0, 0));
    }
    
    ?>
    

    Note that echo outputs to the client.

    But you want to decide what you get when you call flip, and that is done using return.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制