How to pass value to variable and return it without calling recursive loop function?

后端 未结 1 1596
星月不相逢
星月不相逢 2021-01-26 03:12
function test_loop($x_values,$x, $y)
{
    $x = $x + 1;
    if($x < 4)
    {
        //I want to add $x value into $x_values variable, eg : $x_values = $x_values . $x         


        
1条回答
  •  悲&欢浪女
    2021-01-26 03:47

    Why don't you try to make $x_values an array, and add values in $x_values as array items, $x_values[] = $x; and then when you're ready, just implode() those values into a string. Like this:

    function test_loop($x_values,$x, $y)
    {
        $x = $x + 1;
        if($x < 4)
        {
            $x_values[] = $x;
        }
    
    //loop again if y is not = 3;
    $y = $y + 1;
    if($y < 3)
    {
        echo "kkk" . $y . "
    "; $x_values[] = $x; }else{ echo "---------------------
    "; } return implode($x_values); }

    Just make sure that you also pass $x_values as an array initially:

    function abc(){
        $bababa = test_loop([0],1,0);
    
        echo $bababa;
    }
    

    0 讨论(0)
提交回复
热议问题