Function not returning global

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
onei0087
Forum Newbie
Posts: 1
Joined: Thu Nov 03, 2005 12:42 am

Function not returning global

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>";
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Have you closed the braces on that for loop?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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"
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

OK guys, enough off-topic ;) --- even though I started it :?
Post Reply