Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    70
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Simple variable doesn't work!

    Here's a the code from which im 'extracting' a value. Look for the variable im making called $bob, its highlighted in red.
    PHP Code:
    <?
    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" ) {

                [
    B][U][COLOR="Red"]$bob htmlspecialcharstrim$this->title ) );[/COLOR][/U][/B]
                
                
                
                
    $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($fp4096))    {

                
    // parse the data
                
    xml_parse$xml_parser$datafeof($fp) ) or die( sprintf"XML error: %s at line %d"xml_error_stringxml_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 );
    [
    B][U][COLOR="Red"]echo $bob;[/COLOR][/U][/B]
    ?>
    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)

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well, unless you declare $bob somewhere else, I'm afraid he only exists within the scope of that if control structure.
    Omnis mico antequam dominus Spookster!

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    70
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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'.

  • #4
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well, this is called variable scope. It's not all that complicated (you'll kick yourself when you see what I mean).

    PHP Code:
    $bob "value";
    function 
    a(){
       
    $bob "something else";
    }
    echo 
    $bob;//outputs "something else" 
    However:
    Code:
    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
    Last edited by Mhtml; 02-04-2007 at 01:51 PM.
    Omnis mico antequam dominus Spookster!

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    Last edited by Mhtml; 02-04-2007 at 01:47 PM.
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •