By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,892 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,892 IT Pros & Developers. It's quick & easy.

question about variable's scope

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

Feb 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.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.

Feb 27 '07 #2

P: n/a
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.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.
js*******@attglobal.net
==================
Feb 27 '07 #3

P: n/a
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.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.
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?

Feb 27 '07 #4

P: n/a
On Tue, 27 Feb 2007 05:06:02 +0100, Jerry Stuckle
<js*******@attglobal.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.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/
Feb 27 '07 #5

P: n/a
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($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!
Feb 27 '07 #6

P: n/a
On Feb 27, 4:56 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
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($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.

Feb 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.