Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
function test_loop($x_values,$x, $y)
{
    $x = $x + 1;
    if($x < 4)
    {
        //add new $x value to variable $x_values
        $x_values = test_loop($x_values . $x . "##", $x, $y);
    }

    //loop again if y is not = 3;
    $y = $y + 1;
    if($y < 3)
    {
        echo "kkk" . $y . "<br/>";
        $x_values = test_loop($x_values . $x . "##", $x, $y);
    }else{
        echo "---------------------<br/>";
    }

    return $x_values;
}

function abc(){
    $bababa = test_loop(0,1,0);

    echo $bababa;
}

abc();

Output :
kkk1
kkk2
---------------------
kkk1
kkk2
---------------------
kkk1
kkk2
---------------------
kkk2
---------------------
02##3##4##5##3##4##2##3##4##3##

How to make the output become :
kkk1
kkk2
---------------------
02##3##


What I have tried:

Regarding the comment tag
//add new $x value to variable $x_values

I want to add value of $x into variable $x_values by this code :
$x_values = $x_values . $x;


but it is not working, so I force to use
$x_values = test_loop($x_values . $x . "##", $x, $y);
end up it keep calling test_loop(). How to add in value of $x into variable $x_values without calling the test_loop() function?
Posted
Updated 29-Apr-20 15:12pm
v3

1 solution

Ok solution is using array instead of variable :
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 . "<br/>";
    $x_values[] = $x;
}else{
    echo "---------------------<br/>";
}

return implode($x_values); 
}


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

    echo $bababa;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900