variable scope problem

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
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

variable scope problem

Post by itsmani1 »

I am parsing an xml file and after the parsing is done i am saving it in a variable called $tt but at the end when i print it don't work. I think problem is with scope of variable. when i try echo it works but if is put it in var and then print at the end it simple don't , is there any way?

Code: Select all

<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="40%" bgcolor="#09276D"><span class="style1">Event</span></td>
    <td width="20%" bgcolor="#09276D" class="style1">Date</td>
    <td width="20%" bgcolor="#09276D" class="style1">Venue</td>
    <td width="20%" bgcolor="#09276D">&nbsp;</td>
  </tr>

<?PHP
$file = "tst1.xml";
function startElement($parser, $name, $attrs)
{
   if($name == "ROW")
	{
//		echo "<tr><td bgcolor=\"$bg\">".$attrs['THISNAME']."</td><td bgcolor=\"$bg\">".$attrs['THISID']."</td><td bgcolor=\"$bg\">".$attrs['ST']."</td><td bgcolor=\"$bg\" align=\"center\"><img src=\"images/ViewTicket.gif\" width=\"93\" height=\"16\" /></td><tr>";
		$tt = "<tr><td bgcolor=\"$bg\">".$attrs['THISNAME']."</td><td bgcolor=\"$bg\">".$attrs['THISID']."</td><td bgcolor=\"$bg\">".$attrs['ST']."</td><td bgcolor=\"$bg\" align=\"center\"><img src=\"images/ViewTicket.gif\" width=\"93\" height=\"16\" /></td><tr>";
	}
}
function endElement($parser, $name)
{
}
function characterData($parser, $name)
{
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($file,"r");
$data = fread($fp,4096);
xml_parse($xml_parser, $data);
xml_parser_free($xml_parser);

?>
<?PHP echo $tt; ?>
</table>
<style type="text/css">
<!--
.style1 {
	color: #FFFFFF;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
}
-->
</style>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You're exactly right, $tt only exists in the context of the function. Can't you simply print it from the function like you were saying? If not, check this out.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

yes tried it with global and working now
Post Reply