Recursionalism

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • blessblessbless@gmail.com

    Recursionalism

    Hey all,
    I am self taught in PHP and my method is just trial and error most of
    the time, but when trying doe not help anymore I get frustrated... I'm
    sure some of you know what I mean.

    My problem is with a recursive(refer ring back to itself) function I am
    trying to write.
    The function works well on its own, and it also works recursively as
    many times as I want it to.
    I am going to give you a lite version of the function because it
    doesn't really matter what it does.

    global $counter;
    $counter=0;
    function recursion($valu e){
    $counter++;
    if ($counter 10){exit("Recur sion exceeded limit!");}

    $newvalue= blabla something with $value

    if (condition applies){recurs ion($newvalue); }
    return $newvalue;
    $counter=0;
    }

    SO my problem is, the counter is not working. I know it has something
    to do with the fact that functions use their own variables, buts that
    is why I thought to set the counter as global :/
    Anybody have an idea why it wont stop and my apache crashes when I try
    to repeat the function limitless? (ok i know why it crashes, just not
    why the function does not stop;

  • Jon Slaughter

    #2
    Re: Recursionalism


    <blessblessbles s@gmail.comwrot e in message
    news:1178239947 .037795.248560@ o5g2000hsb.goog legroups.com...
    Hey all,
    I am self taught in PHP and my method is just trial and error most of
    the time, but when trying doe not help anymore I get frustrated... I'm
    sure some of you know what I mean.
    >
    My problem is with a recursive(refer ring back to itself) function I am
    trying to write.
    The function works well on its own, and it also works recursively as
    many times as I want it to.
    I am going to give you a lite version of the function because it
    doesn't really matter what it does.
    >
    global $counter;
    $counter=0;
    function recursion($valu e){
    $counter++;
    if ($counter 10){exit("Recur sion exceeded limit!");}
    >
    $newvalue= blabla something with $value
    >
    if (condition applies){recurs ion($newvalue); }
    return $newvalue;
    $counter=0;
    }
    >
    Your counter is not global. you are creating a new counter variable each
    recursive step that is local to the function. So the the first if statement
    never does anything.
    use global $counter inside the function

    global $counter;
    $counter=0;

    function recursion($valu e){
    global $counter;
    $counter++;
    if ($counter 10){exit("Recur sion exceeded limit!");}

    $newvalue= blabla something with $value

    if (condition applies){recurs ion($newvalue); }
    return $newvalue;
    $counter=0;
    }

    Your last line $counter=0 is also not doing anything. your returning so
    anything after that point isn't going to be called. Its also probably not a
    good idea to do that unless you know exactly what your doing. (in this case
    its ok because it doesn't ever get called but in some circumstances it could
    end up causing an infinite loop).

    Jon



    Comment

    • Norman Peelman

      #3
      Re: Recursionalism

      blessblessbless @gmail.com wrote:
      Hey all,
      I am self taught in PHP and my method is just trial and error most of
      the time, but when trying doe not help anymore I get frustrated... I'm
      sure some of you know what I mean.
      >
      My problem is with a recursive(refer ring back to itself) function I am
      trying to write.
      The function works well on its own, and it also works recursively as
      many times as I want it to.
      I am going to give you a lite version of the function because it
      doesn't really matter what it does.
      >
      global $counter;
      $counter=0;
      function recursion($valu e){
      $counter++;
      if ($counter 10){exit("Recur sion exceeded limit!");}
      >
      $newvalue= blabla something with $value
      >
      if (condition applies){recurs ion($newvalue); }
      return $newvalue;
      $counter=0;
      }
      >
      SO my problem is, the counter is not working. I know it has something
      to do with the fact that functions use their own variables, buts that
      is why I thought to set the counter as global :/
      Anybody have an idea why it wont stop and my apache crashes when I try
      to repeat the function limitless? (ok i know why it crashes, just not
      why the function does not stop;
      >
      function recursion($valu e){
      static $counter;
      $counter++;
      if ($counter 10){exit("Recur sion exceeded limit!");}

      $newvalue= blabla something with $value

      if (condition applies){recurs ion($newvalue); }
      return $newvalue;
      $counter=0;
      }

      $the_value = recursion(5);

      The 'static' keyword causes the $counter variable to retain it's value.

      Norm

      Comment

      • Mohawk Moak

        #4
        Re: Recursionalism

        this method worked good, i did not realize to set the global twice.
        the reason to set the counter to 0 in the end is so that the function
        is usable more than 10 times (or whatever limit it is)
        thanks for pointing out that it was in the wrong row though xD
        probably would not have figuered that one out so fast
        bless

        Comment

        • Jon Slaughter

          #5
          Re: Recursionalism


          "Mohawk Moak" <blessblessbles s@gmail.comwrot e in message
          news:1178249760 .084436.255930@ c35g2000hsg.goo glegroups.com.. .
          this method worked good, i did not realize to set the global twice.
          the reason to set the counter to 0 in the end is so that the function
          is usable more than 10 times (or whatever limit it is)
          thanks for pointing out that it was in the wrong row though xD
          probably would not have figuered that one out so fast
          bless
          >
          Yeah. See Normans post about static variables. Normally this is what you use
          but I didn't know if php had it(though that is what global was).

          If you get a program like zend studio then you can debug your code and step
          through it one line at a time and see whats going on. This is how I found
          out that your counter was local and not actually counting the recursive
          steps. It makes it much easier to write code if you can debug it.

          Jon


          Comment

          • gosha bine

            #6
            Re: Recursionalism

            On 04.05.2007 05:36 Mohawk Moak wrote:
            this method worked good, i did not realize to set the global twice.
            the reason to set the counter to 0 in the end is so that the function
            is usable more than 10 times (or whatever limit it is)
            thanks for pointing out that it was in the wrong row though xD
            probably would not have figuered that one out so fast
            bless
            >
            Actually, you only need 'global' once: inside the function. Moreover,
            even this is not really necessary - using global variables is considered
            bad practice and it's better to avoid them from the beginning. This is
            how you can rewrite your function without globals:

            function recursion($valu e, $counter = 0){
            $counter++;
            if ($counter 10){exit("Recur sion exceeded limit!");}

            $newvalue= blabla something with $value

            if (condition applies){recurs ion($newvalue, $counter);}
            return $newvalue;
            }


            --
            gosha bine

            extended php parser ~ http://code.google.com/p/pihipi
            blok ~ http://www.tagarga.com/blok

            Comment

            • Toby A Inkster

              #7
              Re: Recursionalism

              Mohawk Moak wrote:
              this method worked good, i did not realize to set the global twice.
              You don't -- you only need to use it inside the function. Jon's use of
              "global" outside the function is entirely redundant, though doesn't cause
              any harm.

              --
              Toby A Inkster BSc (Hons) ARCS
              Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

              Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

              Comment

              • Rami Elomaa

                #8
                Re: Recursionalism

                "Toby A Inkster" <usenet200703@t obyinkster.co.u kwrote in message
                news:na6rg4-8oj.ln1@ophelia .g5n.co.uk...
                Mohawk Moak wrote:
                >
                >this method worked good, i did not realize to set the global twice.
                >
                You don't -- you only need to use it inside the function. Jon's use of
                "global" outside the function is entirely redundant, though doesn't cause
                any harm.

                If you want to get technical, it's actually not even redundant, since it
                does _nothing_. If it repeated something that already was done, then it
                would be redundant.

                And as for the harm, since it does nothing but litter the source code,
                better take it out. That way it doesn't confuse the next person reading the
                source...

                --
                Rami.Elomaa@gma il.com

                "Good tea. Nice house." -- Worf


                Comment

                • Jon Slaughter

                  #9
                  Re: Recursionalism


                  "Toby A Inkster" <usenet200703@t obyinkster.co.u kwrote in message
                  news:na6rg4-8oj.ln1@ophelia .g5n.co.uk...
                  Mohawk Moak wrote:
                  >
                  >this method worked good, i did not realize to set the global twice.
                  >
                  You don't -- you only need to use it inside the function. Jon's use of
                  "global" outside the function is entirely redundant, though doesn't cause
                  any harm.
                  >
                  Oh, lol... I didn't read is post very well. Yes it is redudant. Didn't mean
                  it to be there. In any case its probably best to use static. In this case
                  it might work but even redudancy can cause problems.


                  Comment

                  • Mohawk Mawk

                    #10
                    Re: Recursionalism

                    i find goshas solution most elegant, and i am all about trying to keep
                    my code clean, so thanks to all again

                    Comment

                    Working...