Page 1 of 1

Function not returning global

Posted: Thu Nov 03, 2005 1:08 am
by onei0087
Very new to coding in general...this one I just can't figure out.
This is for parsing out XML data.... using three functions startElement, endElement and characterData :

Code: Select all

function startElement ($parser,$name,$attrib){
     	global $usercount;
     	global $franchise;
     	global $userdata;
     	global $state;
     
	    switch ($name) {
    	case $name=="FRANCHISE" : {
     		$userdata[$franchise]["franchise"] = $attrib["ID"];
 		}
     	break;
     	
     	default : {$state=$name; break;}
 		}
}
function endElement ($parser,$name){
     	global $usercount;
     	global $franchise;
     	global $userdata;
     	global $state;
     	
     	$state='';
     	if($name=="FRANCHISE") {$franchise++;}
}

function characterData ($parser, $data) {
     	global $usercount;
     	global $userdata2;
     	global $franchise;
     	global $state;
     	
     	$userdata2[$franchise]["$state"] = $data;
     	if (!$state) {return;}
 		if ($state=="H2HL") {
	 		$userdata2[$franchise]["h2hl"] = $data;
 			$h2hl = $userdata2[$franchise]["h2hl"];
 			echo "$data , $state, $franchise , $h2hl<p>";    //This is solely for testing.
		}
}
     	$usercount = 0;
     	$franchise = 0;
     	$userdata = array();
     	$userdata2 = array();
     	$state = '';

     	$simpleparser = xml_parser_create('UTF-8');
     	xml_set_element_handler($simpleparser, "startElement", "endElement");
     	xml_set_character_data_handler($simpleparser, "characterData");
     	if (!($fp = fopen($file, "r"))) {
         	die("could not open XML input");
     	}
     
     	while (!feof($fp)) {
        	 $data = fgets($fp);
         	if (!xml_parse($simpleparser, $data, feof($fp))) {
            	 die(xml_error_string(xml_get_error_code($simpleparser)));
         	}
     	}
     	
     	for ($i=0;$i<$franchise;$i++) {   
	     	$h2hw = $userdata[$i]["franchise"];
	     	$h2hl=$userdata2[$i]["h2hl"];	

 Line 132     	echo "<p>Season: $request_season Week: $week Record: <strong>$h2hw - $h2hl</strong></p><hr noshade>";
The output and the problem I am encountering is:
Notice: Undefined variable: h2hl in /home/content/o/n/e/onei0087/html/Thickbook/standings_parser.php on line 132

Season: 2005 Week: 9 Record: 0001 -

So I know that I am getting $h2hw from startElement, but why am I unable to get $h2hl from characterData?

Posted: Thu Nov 03, 2005 4:42 am
by Chris Corbyn
I couldn't follow your code very easily but looking at this, that error seems very odd unless $franchise is zero at this point :? But then $h2hw is defined....

Has anything else some to light? It looks like you're going the right way about debugging, by echoing things in your functions etc...

Code: Select all

for ($i=0;$i<$franchise;$i++) {   
             $h2hw = $userdata[$i]["franchise"];
             $h2hl=$userdata2[$i]["h2hl"];    

Line 132         echo "<p>Season: $request_season Week: $week Record: <strong>$h2hw - $h2hl</strong></p><hr noshade>";

Posted: Thu Nov 03, 2005 5:40 am
by Jenk
Have you closed the braces on that for loop?

Posted: Thu Nov 03, 2005 5:53 am
by redmonkey
<off topic>
d11wtq wrote:It looks like you're going the right way about debugging, by echoing things in your functions etc...
That's not the right way to debug.
</off topic>

Posted: Thu Nov 03, 2005 6:05 am
by Chris Corbyn
redmonkey wrote:<off topic>
d11wtq wrote:It looks like you're going the right way about debugging, by echoing things in your functions etc...
That's not the right way to debug.
</off topic>
What's the *right* way? OK, it's one way :)

Don't say unit testing because I can't argue LOL :P

Posted: Thu Nov 03, 2005 6:12 am
by redmonkey
I consider echoing things out to be the "poor man's" debugger, granted it gets the job done but it's not efficient. Not as bad in this case maybe, but in a large scale application it's quite easy to accidentally leave a stray echo in there somwhere and you could spend quite some time tracking it down.

A true debugger will allow you to 'watch' variables, set break points, step in, out and over of the code and can also show which variables are available in the current scope. It makes life a lot easier (for me at least) and the debugging process is far more efficient.

Posted: Thu Nov 03, 2005 6:54 am
by Jenk
Which is not possible in PHP without the use of a 3rd party application and to my knowledge the only one that has this functionality is a pay-for app, thus echoing the variables is a perfectly acceptable way to debug. If you leave a stray echo somewhere, more fool you for not noticing it in testing and thus leaving it behind :)

Posted: Thu Nov 03, 2005 6:59 am
by redmonkey
There are several PHP IDEs that have this functionality and I know of one which is freeware :P (can't remember it's name though (I think it incorporates DBG))

I don't quite understand the logic behind your comment RE "pay-for app"

Posted: Thu Nov 03, 2005 7:05 am
by Jenk
It's an app that you have to pay for, thus it is a pay-for app.

And the argument of debugging is void as there is not "right" way about it aslong as the bugs get taken out. echoing variables is fine.

Posted: Thu Nov 03, 2005 7:10 am
by redmonkey
I understand the term "pay-for" app but I'm unclear as to the logic of being so rules it out of the equation.

Note: I did acknowledge that echoing gets the job done but it is my opinion that it is not the correct/right way to go about it.

Posted: Thu Nov 03, 2005 7:13 am
by Jenk
pay-for apps are less likely to be bought than an app you can get for free, unless the app is 'x' more rewarding than the free app.

And echoing variables is perfectly fine for debugging.. how else are you going to know what is in the variables?

Posted: Thu Nov 03, 2005 7:21 am
by redmonkey
Jenk wrote:pay-for apps are less likely to be bought than an app you can get for free, unless the app is 'x' more rewarding than the free app.

And echoing variables is perfectly fine for debugging.. how else are you going to know what is in the variables?
hmm.. use a 'true' debugger app? lol

As I stated, there are freeware IDEs which offer this functionality.

Posted: Thu Nov 03, 2005 9:49 am
by Chris Corbyn
OK guys, enough off-topic ;) --- even though I started it :?