|
Posted by finer recliner on 02/27/07 15:05
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.
Navigation:
[Reply to this message]
|