3

I have a function like:

myfunction($i,$condition = false, $level = 0) {
   do {
      if (... some conditions here)   { myfunction($i, true, ++$level) }
      else { do something here ... }
   while ( ...meet ending condition )
}

I don't understand why the $condition turn true when i call myfunction() recursively and come back to false when iterating in first level and $level won't turn to 0 after it exits a recursive mode.

$condition = false, false, true, false, false, true, true, true ...

$level = 0,0,1,1,1,2,2,2 ... it shoul also be like = 0,0,1,0,0,1,2,2,2,0 ... and so on ?

Thank you

P.S : It is the same with arrays ? I declared an array in the function set to null and when exits the recursive mode it's not null anymore :

myfunction($i,$condition = false, $level = 0, $array = null) {
       do {
    if($condition) { $array = null }    <--------- I HAVE TO ADD THIS LINE TO MAKE IT NULL WHY ?
          if (... some conditions here)   {$array = Array(someblabla); myfunction($i, true, ++$level, $array) }
          else { do something here ... }
       while ( ...meet ending condition )
    }
3
  • Thanks to all ... $level+1 did the trick as you said. But i don't know why and where is menitoned in the php`s references . Commented Feb 8, 2012 at 13:17
  • php.net/manual/en/language.operators.increment.php Commented Feb 8, 2012 at 13:19
  • Can someone reply to the P.S in Question ? Thanks Commented Feb 8, 2012 at 13:35

3 Answers 3

2

What you're missing is the difference between ++$level and $level+1. The former modifies the value of $level, so that further references to that variable in the same invocation of myfunction see the incremented value. If that's not what you want, write $level+1 instead.

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

6 Comments

Genius ... I never understood that . Thank you very much , you saved my brains becaus I started to get frustrated.
It's the same way with arrays ? because I added an array to the function and when exits recursive mode ... the $array is not null anymore .. it was declared myfucntion($i, $condition = false, $array = null, $level = 0) ? Thank you very much
I think we'd need to see a bit more of the code to understand exactly what's happening with $array. Is there anything else that sets $array? Are there any other calls to myfunction that provide that fourth parameter?
the array is set if it met a condition and then function enters recursive mode with the array set, and when it end the recursivity , the array won't turn back to it's null value . I have describen in my question this (P.S)
Well, after you set $array explicitly, further uses of it in that invocation of myfunction -- e.g., in later iterations of that do-while loop -- will see the new value. Every time you call the function, you effectively get new versions of all its local variables (including the function arguments), which evaporate when the function returns. So: you call myfunction (call this Call 1). Its $array is null. It then sets $array to something else, and then calls myfunction again (call this Call 2). Its $array also starts out null. Then Call 2 returns, and back in Call 1 [... continued]
|
2

Each executed function has its own local variables. As the name says, these variables are local, not shared between recursive calls.

the ++ operator increments the local variable.

3 Comments

I think what pufos is puzzled by is the fact that after the recursive call has returned, $level still has an increased value -- not the fact that separate calls to myfunction see different things. But the question isn't perfectly clear, so I may have misunderstood.
(Note: at the point when I wrote the above comment, yi_H's answer did not mention the fact that "the ++ operator increments the local variable".)
yepp.. weird that SO doesn't show that the answer was edited.
2

This is happening because you are doing ++$level which increments the local copy of $level and then passes the new incremented value to the recursive call of the function.

Try changing it to $level + 1 which just passes value of $value plus one to the function but does not change the local copy of the variable, so that if the function returns you still have the old un-incremented value in $value.

2 Comments

Thank you very much , that did the trick . But can you explai why ?
Ok I understood it clea now ... You are perfecty right and make sense.

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.