Click here to Skip to main content
14,640,737 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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;
}
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100