Finite $_POST loops infinitely - help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    Finite $_POST loops infinitely - help!

    I have no idea why this is happening and I need someone to explain this
    to me at the simplest level absolutely possible (pretend I'm a 10-year
    old and explain it that way, please!)

    This class method:

    PHP Code:
    /**
    * Perform an array scan
    *
    * @access private
    * @param array $array
    * @see vname
    */
    function &array_scan(&$a rray) {
    if (is_array($arra y) && @sizeof($array) 0) {
    print_r("sizeof (" . vname($array) . ") = " . sizeof($array) .
    "<P>");
    $index = 1;
    foreach ($array as $key =$val) {
    print_r("index = $index<P>"); $index++;
    $this->setData($val );
    print_r("key = $key and val = $val and this->data =
    $this->data and array name = " . vname($array) . "<P>");
    $this->scan($key, vname($array));
    $array[$key] = $this->getData();
    }
    }
    }


    Constantly produces the following results:

    Quote:
    sizeof(_POST) = 5

    index = 1

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 2

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 3

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 4

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 5

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 6

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 7

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 8

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 9

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    index = 10

    key = username and val = phillip and this->data = phillip and array
    name = _POST

    this->data = phillip

    ....// and so on and so on.. as high as 200,000 at times and still
    doesn't quit!!



    Why is this happening, I honestly can't see why.

    Thanx
    Phil

  • flamer die.spam@hotmail.com

    #2
    Re: Finite $_POST loops infinitely - help!

    function &array_scan(&$a rray) {
    if (is_array($arra y) && @sizeof($array) 0) {
    print_r("sizeof (" . vname($array) . ") = " . sizeof($array) .
    "<P>");

    your looping for any number higher than zero.

    Flamer.

    Comment

    • comp.lang.php

      #3
      Re: Finite $_POST loops infinitely - help!

      HUH??

      Phil

      flamer die.spam@hotmai l.com wrote:
      function &array_scan(&$a rray) {
      if (is_array($arra y) && @sizeof($array) 0) {
      print_r("sizeof (" . vname($array) . ") = " . sizeof($array) .
      "<P>");
      >
      >
      your looping for any number higher than zero.
      >
      Flamer.

      Comment

      • Andrew Poelstra

        #4
        Re: Finite $_POST loops infinitely - help!

        "comp.lang. php" <phillip.s.powe ll@gmail.comwri tes:
        I have no idea why this is happening and I need someone to explain this
        to me at the simplest level absolutely possible (pretend I'm a 10-year
        old and explain it that way, please!)
        >
        This class method:
        >
        PHP Code:
        /**
        * Perform an array scan
        *
        * @access private
        * @param array $array
        * @see vname
        */
        function &array_scan(&$a rray) {
        if (is_array($arra y) && @sizeof($array) 0) {
        Instead of indenting the whole function, you may wish to just add a
        premature return and negate the test:
        if (!isarray ($array) || @sizeof ($array) == 0)
        return;
        print_r("sizeof (" . vname($array) . ") = " . sizeof($array) .
        "<P>");
        $index = 1;
        foreach ($array as $key =$val) {
        How about you just indent two more spaces after each {? This style is
        very erratic and hard to read.

        This appears to be your only loop; try replacing the body with something
        simple like
        echo "Key: $key<br />Value: $val<br /><br />\n";
        and see if the problem persists.
        print_r("index = $index<P>"); $index++;
        $this->setData($val );
        print_r("key = $key and val = $val and this->data =
        $this->data and array name = " . vname($array) . "<P>");
        $this->scan($key, vname($array));
        $array[$key] = $this->getData();
        }
        }
        }
        >
        >
        Constantly produces the following results:
        >
        Quote:
        sizeof(_POST) = 5
        >
        index = 1
        >
        key = username and val = phillip and this->data = phillip and array
        name = _POST
        >
        this->data = phillip
        >
        index = 2
        >
        key = username and val = phillip and this->data = phillip and array
        name = _POST
        >
        this->data = phillip
        >
        index = 3
        >
        ...// and so on and so on.. as high as 200,000 at times and still
        doesn't quit!!
        >
        Is $index a reserved variable, or something that foreach() might be
        using internally? I assume not, but try renaming or removing it and
        see if that helps any.

        --
        Andrew Poelstra <http://www.wpsoftware. net/projects>
        To reach me by email, use `apoelstra' at the above domain.
        "Do BOTH ends of the cable need to be plugged in?" -Anon.

        Comment

        • comp.lang.php

          #5
          Re: Finite $_POST loops infinitely - help!


          Andrew Poelstra wrote:
          "comp.lang. php" <phillip.s.powe ll@gmail.comwri tes:
          >
          I have no idea why this is happening and I need someone to explain this
          to me at the simplest level absolutely possible (pretend I'm a 10-year
          old and explain it that way, please!)

          This class method:

          PHP Code:
          /**
          * Perform an array scan
          *
          * @access private
          * @param array $array
          * @see vname
          */
          function &array_scan(&$a rray) {
          if (is_array($arra y) && @sizeof($array) 0) {
          >
          Instead of indenting the whole function, you may wish to just add a
          premature return and negate the test:
          if (!isarray ($array) || @sizeof ($array) == 0)
          return;
          >
          print_r("sizeof (" . vname($array) . ") = " . sizeof($array) .
          "<P>");
          $index = 1;
          foreach ($array as $key =$val) {
          >
          How about you just indent two more spaces after each {? This style is
          very erratic and hard to read.
          >
          This appears to be your only loop; try replacing the body with something
          simple like
          echo "Key: $key<br />Value: $val<br /><br />\n";
          and see if the problem persists.
          >
          print_r("index = $index<P>"); $index++;
          $this->setData($val );
          print_r("key = $key and val = $val and this->data =
          $this->data and array name = " . vname($array) . "<P>");
          $this->scan($key, vname($array));
          $array[$key] = $this->getData();
          }
          }
          }


          Constantly produces the following results:

          Quote:
          sizeof(_POST) = 5

          index = 1

          key = username and val = phillip and this->data = phillip and array
          name = _POST

          this->data = phillip

          index = 2

          key = username and val = phillip and this->data = phillip and array
          name = _POST

          this->data = phillip

          index = 3

          ...// and so on and so on.. as high as 200,000 at times and still
          doesn't quit!!
          >
          Is $index a reserved variable, or something that foreach() might be
          using internally? I assume not, but try renaming or removing it and
          see if that helps any.
          Well, this is what I found out:

          No matter what I put within the foreach loop, the loop ran infinitely,
          and this is why:

          It constantly read $key as the very first element in $array, in short,
          it never iterated in the first place!

          This only happens when I do this;

          function doStuff(&$array ) {
          if (is_array($arra y) && @sizeof($array) 0) {
          foreach ($array as $key =$val) print_r("key = $key<P>"); //
          PRINTS "key = username" infinitely
          }
          }

          --------------------------------------------------------------------------------------------------------------------------------------

          What I suspect at this point that this is a PHP 4.3+ bug. I had
          someone else in my DC PHP group test in PHP 5 and the loop iterated
          just fine.

          If I pass the array not-by-reference in PHP 4.3.9, it iterates just
          fine:

          function doStuff($array) {
          if (is_array($arra y) && @sizeof($array) 0) {
          foreach ($array as $key =$val) print_r("key = $key<P>"); // PRINTS
          "key = username" 5 times and stops
          }
          }

          =============== =============== =============== =============== =======

          Phil
          >
          --
          Andrew Poelstra <http://www.wpsoftware. net/projects>
          To reach me by email, use `apoelstra' at the above domain.
          "Do BOTH ends of the cable need to be plugged in?" -Anon.

          Comment

          • Andrew Poelstra

            #6
            Re: Finite $_POST loops infinitely - help!

            "comp.lang. php" <phillip.s.powe ll@gmail.comwri tes:
            Well, this is what I found out:
            >
            No matter what I put within the foreach loop, the loop ran infinitely,
            and this is why:
            >
            It constantly read $key as the very first element in $array, in short,
            it never iterated in the first place!
            >
            This only happens when I do this;
            >
            function doStuff(&$array ) {
            if (is_array($arra y) && @sizeof($array) 0) {
            foreach ($array as $key =$val) print_r("key = $key<P>"); //
            PRINTS "key = username" infinitely
            }
            }
            >
            >
            What I suspect at this point that this is a PHP 4.3+ bug. I had
            someone else in my DC PHP group test in PHP 5 and the loop iterated
            just fine.
            >
            That's what it looks like. If it's not a bug, then by definition it's a
            documented "feature" in PHP 4.3x, and it'll be somewhere on the site.

            One of the many unfortunate aspects of a bug is that the group can't
            help you, other than to suggest you upgrade your version of PHP.
            If I pass the array not-by-reference in PHP 4.3.9, it iterates just
            fine:
            Another phrase for "not-by-reference" is "by value", FYI.
            >
            function doStuff($array) {
            if (is_array($arra y) && @sizeof($array) 0) {
            foreach ($array as $key =$val) print_r("key = $key<P>"); // PRINTS
            "key = username" 5 times and stops
            }
            }
            >
            Instead of passing by reference, you could pass by value and then return
            the modified version. That appears to be the best solution unless you have
            the power to install PHP5.

            --
            Andrew Poelstra <http://www.wpsoftware. net/projects>
            To reach me by email, use `apoelstra' at the above domain.
            "Do BOTH ends of the cable need to be plugged in?" -Anon.

            Comment

            • comp.lang.php

              #7
              Re: Finite $_POST loops infinitely - help!


              Andrew Poelstra wrote:
              "comp.lang. php" <phillip.s.powe ll@gmail.comwri tes:
              Well, this is what I found out:

              No matter what I put within the foreach loop, the loop ran infinitely,
              and this is why:

              It constantly read $key as the very first element in $array, in short,
              it never iterated in the first place!

              This only happens when I do this;

              function doStuff(&$array ) {
              if (is_array($arra y) && @sizeof($array) 0) {
              foreach ($array as $key =$val) print_r("key = $key<P>"); //
              PRINTS "key = username" infinitely
              }
              }


              What I suspect at this point that this is a PHP 4.3+ bug. I had
              someone else in my DC PHP group test in PHP 5 and the loop iterated
              just fine.
              >
              That's what it looks like. If it's not a bug, then by definition it's a
              documented "feature" in PHP 4.3x, and it'll be somewhere on the site.
              >
              One of the many unfortunate aspects of a bug is that the group can't
              help you, other than to suggest you upgrade your version of PHP.
              >
              I realize this, which is probably it's an undocumented bug and will
              remain that way. Someone tested this in PHP 4.3.10 and it worked
              (passing array by reference and iterating through array), so perhaps
              it's only in PHP 4.3.9 on back.

              If I pass the array not-by-reference in PHP 4.3.9, it iterates just
              fine:
              >
              Another phrase for "not-by-reference" is "by value", FYI.
              >
              Thanx, the terminology escaped me.

              function doStuff($array) {
              if (is_array($arra y) && @sizeof($array) 0) {
              foreach ($array as $key =$val) print_r("key = $key<P>"); // PRINTS
              "key = username" 5 times and stops
              }
              }
              >
              Instead of passing by reference, you could pass by value and then return
              the modified version. That appears to be the best solution unless you have
              the power to install PHP5.
              >
              That's what I wound up doing, though if you wish to change autoglobals
              like $_POST or $_GET you have to remember to make sure to set it to
              equal the value returned by the function or method.

              Phil

              PS: Thanx for your help and insight!
              --
              Andrew Poelstra <http://www.wpsoftware. net/projects>
              To reach me by email, use `apoelstra' at the above domain.
              "Do BOTH ends of the cable need to be plugged in?" -Anon.

              Comment

              Working...