14,640,737 members
Rate this:
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

Rate this:

## 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