0

My recursive function is

function recursion($vals,$i) //
{
$htm=implode('',file($vals));
echo $i;
if($htm)
{
return $htm;
}
else
{
    echo "\n.................link broken...................\n";
    sleep(10);
    echo "\n.................retrying......................\n";     
    **// return recursion($vals,$i+1);  //case 1
            // return recursion($vals,$i++);    //case 2**
}
}

using case 1 is incrementing value of $i+1 while using $i++ not increamenting $i value why ?

1 Answer 1

2

Because post-increment ($a++) operator means

Returns $a, then increments $a by one.

Check out official document.

Addition:

And (as @Matt states) pre-increment operator (++$a) means increment and then return.

So in your case, you used

recursion($i++);

Which results in $i returning its current value into function call's parameter, and then increment itself by one, so recursion() will always get the same $i because it is incremented after it is used as a parameter.

Change to

recursion(++$i);

Would help.

Sign up to request clarification or add additional context in comments.

1 Comment

And the pre-increment operator (++$a) means increment by one and then return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.