question about variable's scope 
February 27th, 2007, 02:15 AM
| | | 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.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////////////////////////////////////////// | 
February 27th, 2007, 02:25 AM
| | | 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. | 
February 27th, 2007, 03:15 AM
| | | 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
================== | 
February 27th, 2007, 03:25 AM
| | | 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:
/////////////////////////start code ////////////////////////////////
<html>
<head>
<?php
if ($submit) {
$newdata = addHTML($newdata);
$fp = fopen("data.txt", "w");
fwrite($fp, $newdata);
fclose($fp);
| >> Quote:
$fcurrent = fopen("newpage.html", "r");
$i = 0;
while(!feof($fcurrent)){
$line = fgets($fcurrent, 4096);
$above .= $line;}
| >> Quote:
function addHTML($text){
global $above;
$newdata = $above . $text;
return $newdata;
| >> 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? | 
February 27th, 2007, 06:55 AM
| | | 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/ | 
February 27th, 2007, 09:15 AM
| | | 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! | 
February 27th, 2007, 02:15 PM
| | | 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. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|