| 
	
 | 
 Posted by Toby A Inkster on 02/27/07 09:56 
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!
 
  
Navigation:
[Reply to this message] 
 |