...

View Full Version : Simple variable doesn't work!



amir103
02-04-2007, 11:34 AM
Here's a the code from which im 'extracting' a value. Look for the variable im making called $bob, its highlighted in red.

<?
class RSSParser3 {

var $title = "";
var $link = "";
var $description = "";
var $inside_item = false;


var $all_rss_urls = array();

function startElement( $parser, $name, $attrs='' ){
global $current_tag;

$current_tag = $name;

if( $current_tag == "ITEM" )
$this->inside_item = true;

} // endfunc startElement

function endElement( $parser, $tagName, $attrs='' ){
global $current_tag;

if ( $tagName == "ITEM" ) {

$bob = htmlspecialchars( trim( $this->title ) );



$this->title = "";
$this->description = "";
$this->link = "";
$this->inside_item = false;

}

} // endfunc endElement

function characterData( $parser, $data ){
global $current_tag;

if( $this->inside_item ){
switch($current_tag){

case "TITLE":
$this->title .= $data;
break;
case "DESCRIPTION":
$this->description .= $data;
break;
case "LINK":
$this->link .= $data;
break;

default:
break;

}

}

}

function parse_results( $xml_parser, $rss_parser, $file ) {

xml_set_object( $xml_parser, &$rss_parser );
xml_set_element_handler( $xml_parser, "startElement", "endElement" );
xml_set_character_data_handler( $xml_parser, "characterData" );

$fp = fopen("$file","r") or die( "Error reading XML file, $file" );

while ($data = fread($fp, 4096)) {

// parse the data
xml_parse( $xml_parser, $data, feof($fp) ) or die( sprintf( "XML error: %s at line %d", xml_error_string( xml_get_error_code($xml_parser) ), xml_get_current_line_number( $xml_parser ) ) );

}

fclose($fp);

xml_parser_free( $xml_parser );

}

function show_title( $rss_url3 ){

}

function show_list_box( $rss_url3 ){


}

}

global $rss_url3;

// Set a default feed
if( $rss_url3 == "" )
$rss_url3 = "http://localhost/knn/knn_feed/knn_feed_text/feed3.xml";

$xml_parser = xml_parser_create();
$rss_parser = new RSSParser3();

$rss_parser->show_title( $rss_url3 );
$rss_parser->parse_results( $xml_parser, &$rss_parser, $rss_url3 );
$rss_parser->show_list_box( $rss_url3 );
echo $bob;
?>
and on one of the last lines i try to display it, but an error pops up saying:
Notice: Undefined variable: bob in c:\program files\easyphp1-8\www\knn\knn_rss\rssknn3.php on line 109

Why can't it print out a simple variable when I'm sure that the 'htmlspecialchars( trim( $this->title ) );[/' is acting properly (the value of bob)

Mhtml
02-04-2007, 11:38 AM
Well, unless you declare $bob somewhere else, I'm afraid he only exists within the scope of that if control structure.

amir103
02-04-2007, 11:41 AM
Sorry about the $bob not actually being highlighted in red, i cant change it as the thread editor is acting funny. So how can i get the value of bob out of that 'if' control structure, how can it 'break out'.

Mhtml
02-04-2007, 11:48 AM
Well, this is called variable scope. It's not all that complicated (you'll kick yourself when you see what I mean).



$bob = "value";
function a(){
$bob = "something else";
}
echo $bob;//outputs "something else"


However:


function b(){//[edit:] changed if to function to reflect GJay's post below
$bob = "value";
}
echo $bob;//$bob only existed within the function it was declared in he's gone now..


Official PHP documentation for variable scope (http://us2.php.net/variables.scope)

GJay
02-04-2007, 01:38 PM
scoping doesn't work like that in php, in your second example, 'value' will be echoed.

It's not being inside the 'if' that's the problem, it's inside a function inside a class.
You could assign bob to '$this->bob' inside the function, and then access it via $rss_parser->bob, or provide a getter function, or return bob from the function, or pass bob in by reference. There are any number of ways of doing it.

Mhtml
02-04-2007, 01:42 PM
Well I'm not afraid to admit it when I'm wrong, being a C++ programmer primarily, I just concluded that (seeing as it's loosely C syntax) that the same rules applied in respect to variable scope. I stand corrected and I'm actually shocked to see that it would actually work, that is just darn right ridiculous and illogical... that sort of functionality promotes bad code.

Sorry about that misinformation, but yes it is just a matter of scope.. Like I explained (which apparently works) except the level is lower than that constrained to functions and classes.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum