question about variable's scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • finer recliner

    question about variable's scope

    i've been playing with the below code for a while now, and i cant get
    it to work correctly. the user should be able to input text into a
    textbox and upon hitting submit, it will append that text to whatever
    is in "newpage.ht ml" and write this new chunk of text to data.txt.

    the final result of my code will be a bit more elaborate, but i can't
    even get this to work. i believe soemthing is wrong the scope of my
    variables or how my functions are written or something. it seems
    everytime i hit submit, only $text is written to data.txt. $above
    seems to be blank in the addHTML function. any suggestions?

    thanks!
    -dave


    /////////////////////////start code ////////////////////////////////
    <html>
    <head>
    <?php
    if ($submit) {
    $newdata = addHTML($newdat a);
    $fp = fopen("data.txt ", "w");
    fwrite($fp, $newdata);
    fclose($fp);
    }

    $fcurrent = fopen("newpage. html", "r");
    $i = 0;
    while(!feof($fc urrent)){
    $line = fgets($fcurrent , 4096);
    $above .= $line;
    }
    fclose($fcurren t);

    function addHTML($text){
    global $above;
    $newdata = $above . $text;
    return $newdata;
    }//end addHTML

    ?>
    </head>
    <body>
    <form action="<? print $PHP_SELF; ?>" method="post">
    <textarea name="newdata" rows="30" cols="80"></textarea>
    <br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>
    //////////////////////////////////end
    code//////////////////////////////////////////

  • Klarth

    #2
    Re: question about variable's scope

    On Feb 27, 11:59 am, "finer recliner" <finerrecli...@ gmail.comwrote:
    i've been playing with the below code for a while now, and i cant get
    it to work correctly. the user should be able to input text into a
    textbox and upon hitting submit, it will append that text to whatever
    is in "newpage.ht ml" and write this new chunk of text to data.txt.
    >
    the final result of my code will be a bit more elaborate, but i can't
    even get this to work. i believe soemthing is wrong the scope of my
    variables or how my functions are written or something. it seems
    everytime i hit submit, only $text is written to data.txt. $above
    seems to be blank in the addHTML function. any suggestions?
    >
    thanks!
    -dave
    >
    /////////////////////////start code ////////////////////////////////
    <html>
    <head>
    <?php
    if ($submit) {
    $newdata = addHTML($newdat a);
    $fp = fopen("data.txt ", "w");
    fwrite($fp, $newdata);
    fclose($fp);
    >
    }
    >
    $fcurrent = fopen("newpage. html", "r");
    $i = 0;
    while(!feof($fc urrent)){
    $line = fgets($fcurrent , 4096);
    $above .= $line;}
    >
    fclose($fcurren t);
    >
    function addHTML($text){
    global $above;
    $newdata = $above . $text;
    return $newdata;
    >
    }//end addHTML
    >
    ?>
    </head>
    <body>
    <form action="<? print $PHP_SELF; ?>" method="post">
    <textarea name="newdata" rows="30" cols="80"></textarea>
    <br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>
    //////////////////////////////////end
    code//////////////////////////////////////////
    Look at the order of your statements. You are calling your addHTML
    function before that makes use of the global variable $above, before
    $above gets set.

    Comment

    • Jerry Stuckle

      #3
      Re: question about variable's scope

      finer recliner wrote:
      i've been playing with the below code for a while now, and i cant get
      it to work correctly. the user should be able to input text into a
      textbox and upon hitting submit, it will append that text to whatever
      is in "newpage.ht ml" and write this new chunk of text to data.txt.
      >
      the final result of my code will be a bit more elaborate, but i can't
      even get this to work. i believe soemthing is wrong the scope of my
      variables or how my functions are written or something. it seems
      everytime i hit submit, only $text is written to data.txt. $above
      seems to be blank in the addHTML function. any suggestions?
      >
      thanks!
      -dave
      >
      >
      /////////////////////////start code ////////////////////////////////
      <html>
      <head>
      <?php
      if ($submit) {
      $newdata = addHTML($newdat a);
      $fp = fopen("data.txt ", "w");
      fwrite($fp, $newdata);
      fclose($fp);
      }
      >
      $fcurrent = fopen("newpage. html", "r");
      $i = 0;
      while(!feof($fc urrent)){
      $line = fgets($fcurrent , 4096);
      $above .= $line;
      }
      fclose($fcurren t);
      >
      function addHTML($text){
      global $above;
      $newdata = $above . $text;
      return $newdata;
      }//end addHTML
      >
      ?>
      </head>
      <body>
      <form action="<? print $PHP_SELF; ?>" method="post">
      <textarea name="newdata" rows="30" cols="80"></textarea>
      <br>
      <input type="submit" name="submit" value="Submit">
      </form>
      </body>
      </html>
      //////////////////////////////////end
      code//////////////////////////////////////////
      >
      In addition to Klarth's comments, you shouldn't use global variables.
      If you would have passed $above to this function (as a second parameter)
      your problem would have been more obvious (just one of the reasons not
      to use globals).


      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • finer recliner

        #4
        Re: question about variable's scope

        On Feb 26, 10:12 pm, "Klarth" <kah....@gmail. comwrote:
        On Feb 27, 11:59 am, "finer recliner" <finerrecli...@ gmail.comwrote:
        >
        >
        >
        i've been playing with the below code for a while now, and i cant get
        it to work correctly. the user should be able to input text into a
        textbox and upon hitting submit, it will append that text to whatever
        is in "newpage.ht ml" and write this new chunk of text to data.txt.
        >
        the final result of my code will be a bit more elaborate, but i can't
        even get this to work. i believe soemthing is wrong the scope of my
        variables or how my functions are written or something. it seems
        everytime i hit submit, only $text is written to data.txt. $above
        seems to be blank in the addHTML function. any suggestions?
        >
        thanks!
        -dave
        >
        /////////////////////////start code ////////////////////////////////
        <html>
        <head>
        <?php
        if ($submit) {
        $newdata = addHTML($newdat a);
        $fp = fopen("data.txt ", "w");
        fwrite($fp, $newdata);
        fclose($fp);
        >
        }
        >
        $fcurrent = fopen("newpage. html", "r");
        $i = 0;
        while(!feof($fc urrent)){
        $line = fgets($fcurrent , 4096);
        $above .= $line;}
        >
        fclose($fcurren t);
        >
        function addHTML($text){
        global $above;
        $newdata = $above . $text;
        return $newdata;
        >
        }//end addHTML
        >
        ?>
        </head>
        <body>
        <form action="<? print $PHP_SELF; ?>" method="post">
        <textarea name="newdata" rows="30" cols="80"></textarea>
        <br>
        <input type="submit" name="submit" value="Submit">
        </form>
        </body>
        </html>
        //////////////////////////////////end
        code//////////////////////////////////////////
        >
        Look at the order of your statements. You are calling your addHTML
        function before that makes use of the global variable $above, before
        $above gets set.
        moving the addHTML function to the very beginning of my php block does
        not change anything. I also tried moving it between the if(submit){}
        block and the $fcurrent = fopen(...); line. no change. other ideas?

        Comment

        • OmegaJunior

          #5
          Re: question about variable's scope

          On Tue, 27 Feb 2007 05:06:02 +0100, Jerry Stuckle
          <jstucklex@attg lobal.netwrote:
          finer recliner wrote:
          >i've been playing with the below code for a while now, and i cant get
          >it to work correctly. the user should be able to input text into a
          >textbox and upon hitting submit, it will append that text to whatever
          >is in "newpage.ht ml" and write this new chunk of text to data.txt.
          > the final result of my code will be a bit more elaborate, but i can't
          >even get this to work. i believe soemthing is wrong the scope of my
          >variables or how my functions are written or something. it seems
          >everytime i hit submit, only $text is written to data.txt. $above
          >seems to be blank in the addHTML function. any suggestions?
          > thanks!
          >-dave
          > /////////////////////////start code ////////////////////////////////
          ><html>
          ><head>
          ><?php
          >if ($submit) {
          >$newdata = addHTML($newdat a);
          >$fp = fopen("data.txt ", "w");
          >fwrite($fp, $newdata);
          >fclose($fp);
          >}
          > $fcurrent = fopen("newpage. html", "r");
          >$i = 0;
          >while(!feof($f current)){
          > $line = fgets($fcurrent , 4096);
          > $above .= $line;
          >}
          >fclose($fcurre nt);
          > function addHTML($text){
          > global $above;
          > $newdata = $above . $text;
          > return $newdata;
          >}//end addHTML
          > ?>
          ></head>
          ><body>
          ><form action="<? print $PHP_SELF; ?>" method="post">
          ><textarea name="newdata" rows="30" cols="80"></textarea>
          ><br>
          ><input type="submit" name="submit" value="Submit">
          ></form>
          ></body>
          ></html>
          >//////////////////////////////////end
          >code//////////////////////////////////////////
          >>
          >
          In addition to Klarth's comments, you shouldn't use global variables. If
          you would have passed $above to this function (as a second parameter)
          your problem would have been more obvious (just one of the reasons not
          to use globals).
          >
          >
          Use $_POST[] instead.

          I agree with Klarth: the order of the statements does not compute.
          Suggestion: write down, in small sequential steps, how the script should
          function. Make sure that every next step gets all its data from a previous
          step. Then write the actual script.

          --
          Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

          Comment

          • Toby A Inkster

            #6
            Re: question about variable's scope

            finer recliner wrote:
            moving the addHTML function to the very beginning of my php block does
            not change anything. I also tried moving it between the if(submit){}
            block and the $fcurrent = fopen(...); line. no change. other ideas?
            Moving the *definition* of the function does not change anything. Klarth
            was referring to your *use* of the addHTML() function.

            Firstly, forget about the definition of addHTML(). It's not relevant.
            Imagine that addHTML() is some built-in PHP function so does not need
            defining.

            <?php
            if ($submit) {
            // At this point, you call addHTML().
            // addHTML() tries to read a variable called $above.
            // This $above variable has not been defined yet.
            $newdata = addHTML($newdat a);
            $fp = fopen("data.txt ", "w");
            fwrite($fp, $newdata);
            fclose($fp);
            }
            $fcurrent = fopen("newpage. html", "r");
            $i = 0;
            while(!feof($fc urrent)){
            $line = fgets($fcurrent , 4096);
            // Now you define $above, but it's too late!
            $above .= $line;
            }
            fclose($fcurren t);
            ?>

            The solution is to re-order you code like this:

            <?php
            $fcurrent = fopen("newpage. html", "r");
            $i = 0;
            while(!feof($fc urrent)){
            $line = fgets($fcurrent , 4096);
            $above .= $line;
            }
            fclose($fcurren t);

            if ($submit) {
            $newdata = addHTML($newdat a);
            $fp = fopen("data.txt ", "w");
            fwrite($fp, $newdata);
            fclose($fp);
            }

            function addHTML($text){
            global $above;
            $newdata = $above . $text;
            return $newdata;
            }//end addHTML
            ?>

            It has nothing to do with variable scope. If you do not understand why you
            were having the problem, or why the solution above works, then you
            *urgently* need to purchase a good book on imperative programming
            techniques and read it from start to finish.

            --
            Toby A Inkster BSc (Hons) ARCS
            Contact Me ~ http://tobyinkster.co.uk/contact
            Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

            * = I'm getting there!

            Comment

            • finer recliner

              #7
              Re: question about variable's scope

              On Feb 27, 4:56 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
              wrote:
              finer recliner wrote:
              moving the addHTML function to the very beginning of my php block does
              not change anything. I also tried moving it between the if(submit){}
              block and the $fcurrent = fopen(...); line. no change. other ideas?
              >
              Moving the *definition* of the function does not change anything. Klarth
              was referring to your *use* of the addHTML() function.
              >
              Firstly, forget about the definition of addHTML(). It's not relevant.
              Imagine that addHTML() is some built-in PHP function so does not need
              defining.
              >
              <?php
              if ($submit) {
              // At this point, you call addHTML().
              // addHTML() tries to read a variable called $above.
              // This $above variable has not been defined yet.
              $newdata = addHTML($newdat a);
              $fp = fopen("data.txt ", "w");
              fwrite($fp, $newdata);
              fclose($fp);
              }
              $fcurrent = fopen("newpage. html", "r");
              $i = 0;
              while(!feof($fc urrent)){
              $line = fgets($fcurrent , 4096);
              // Now you define $above, but it's too late!
              $above .= $line;
              }
              fclose($fcurren t);
              ?>
              >
              The solution is to re-order you code like this:
              >
              <?php
              $fcurrent = fopen("newpage. html", "r");
              $i = 0;
              while(!feof($fc urrent)){
              $line = fgets($fcurrent , 4096);
              $above .= $line;
              }
              fclose($fcurren t);
              >
              if ($submit) {
              $newdata = addHTML($newdat a);
              $fp = fopen("data.txt ", "w");
              fwrite($fp, $newdata);
              fclose($fp);
              }
              >
              function addHTML($text){
              global $above;
              $newdata = $above . $text;
              return $newdata;
              }//end addHTML
              ?>
              >
              It has nothing to do with variable scope. If you do not understand why you
              were having the problem, or why the solution above works, then you
              *urgently* need to purchase a good book on imperative programming
              techniques and read it from start to finish.
              >
              --
              Toby A Inkster BSc (Hons) ARCS
              Contact Me ~http://tobyinkster.co.uk/contact
              Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
              >
              * = I'm getting there!
              reordering the script worked this time :) thanks for all your help.

              i understand the reason behind the problem now. variable scope was
              just a guess when i originally posted since my $above seemed to lose
              its value in between statements. anyways thanks again.

              Comment

              Working...