Connecting Tech Pros Worldwide Help | Site Map

question about variable's scope

  #1  
Old February 27th, 2007, 03:15 AM
finer recliner
Guest
 
Posts: n/a
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.html" 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($newdata);
$fp = fopen("data.txt", "w");
fwrite($fp, $newdata);
fclose($fp);
}

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

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//////////////////////////////////////////

  #2  
Old February 27th, 2007, 03:25 AM
Klarth
Guest
 
Posts: n/a

re: question about variable's scope


On Feb 27, 11:59 am, "finer recliner" <finerrecli...@gmail.comwrote:
Quote:
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.html" 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($newdata);
$fp = fopen("data.txt", "w");
fwrite($fp, $newdata);
fclose($fp);
>
}
>
$fcurrent = fopen("newpage.html", "r");
$i = 0;
while(!feof($fcurrent)){
$line = fgets($fcurrent, 4096);
$above .= $line;}
>
fclose($fcurrent);
>
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.

  #3  
Old February 27th, 2007, 04:15 AM
Jerry Stuckle
Guest
 
Posts: n/a

re: question about variable's scope


finer recliner wrote:
Quote:
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.html" 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($newdata);
$fp = fopen("data.txt", "w");
fwrite($fp, $newdata);
fclose($fp);
}
>
$fcurrent = fopen("newpage.html", "r");
$i = 0;
while(!feof($fcurrent)){
$line = fgets($fcurrent, 4096);
$above .= $line;
}
fclose($fcurrent);
>
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@attglobal.net
==================
  #4  
Old February 27th, 2007, 04:25 AM
finer recliner
Guest
 
Posts: n/a

re: question about variable's scope


On Feb 26, 10:12 pm, "Klarth" <kah....@gmail.comwrote:
Quote:
On Feb 27, 11:59 am, "finer recliner" <finerrecli...@gmail.comwrote:
>
>
>
Quote:
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.html" and write this new chunk of text to data.txt.
>
Quote:
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?
>
Quote:
thanks!
-dave
>
Quote:
/////////////////////////start code ////////////////////////////////
<html>
<head>
<?php
if ($submit) {
$newdata = addHTML($newdata);
$fp = fopen("data.txt", "w");
fwrite($fp, $newdata);
fclose($fp);
>
Quote:
}
>
Quote:
$fcurrent = fopen("newpage.html", "r");
$i = 0;
while(!feof($fcurrent)){
$line = fgets($fcurrent, 4096);
$above .= $line;}
>
Quote:
fclose($fcurrent);
>
Quote:
function addHTML($text){
global $above;
$newdata = $above . $text;
return $newdata;
>
Quote:
}//end addHTML
>
Quote:
?>
</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?

  #5  
Old February 27th, 2007, 07:55 AM
OmegaJunior
Guest
 
Posts: n/a

re: question about variable's scope


On Tue, 27 Feb 2007 05:06:02 +0100, Jerry Stuckle
<jstucklex@attglobal.netwrote:
Quote:
finer recliner wrote:
Quote:
>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.html" 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($newdata);
>$fp = fopen("data.txt", "w");
>fwrite($fp, $newdata);
>fclose($fp);
>}
> $fcurrent = fopen("newpage.html", "r");
>$i = 0;
>while(!feof($fcurrent)){
> $line = fgets($fcurrent, 4096);
> $above .= $line;
>}
>fclose($fcurrent);
> 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/
  #6  
Old February 27th, 2007, 10:15 AM
Toby A Inkster
Guest
 
Posts: n/a

re: question about variable's scope


finer recliner wrote:
Quote:
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($newdata);
$fp = fopen("data.txt", "w");
fwrite($fp, $newdata);
fclose($fp);
}
$fcurrent = fopen("newpage.html", "r");
$i = 0;
while(!feof($fcurrent)){
$line = fgets($fcurrent, 4096);
// Now you define $above, but it's too late!
$above .= $line;
}
fclose($fcurrent);
?>

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

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

if ($submit) {
$newdata = addHTML($newdata);
$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!
  #7  
Old February 27th, 2007, 03:15 PM
finer recliner
Guest
 
Posts: n/a

re: question about variable's scope


On Feb 27, 4:56 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
Quote:
finer recliner wrote:
Quote:
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($newdata);
$fp = fopen("data.txt", "w");
fwrite($fp, $newdata);
fclose($fp);
}
$fcurrent = fopen("newpage.html", "r");
$i = 0;
while(!feof($fcurrent)){
$line = fgets($fcurrent, 4096);
// Now you define $above, but it's too late!
$above .= $line;
}
fclose($fcurrent);
?>
>
The solution is to re-order you code like this:
>
<?php
$fcurrent = fopen("newpage.html", "r");
$i = 0;
while(!feof($fcurrent)){
$line = fgets($fcurrent, 4096);
$above .= $line;
}
fclose($fcurrent);
>
if ($submit) {
$newdata = addHTML($newdata);
$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.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about variable scope conflict David Mathog answers 7 December 3rd, 2007 01:55 AM
a question about memory error.. liuhaoran answers 1 December 11th, 2006 01:45 PM
variable scope in C MyCGal answers 4 November 2nd, 2006 10:11 AM
variable scope Michael G answers 7 August 16th, 2005 02:05 PM