Help: Access a variable in a function outside of it...

Discussion in 'PHP' started by eduardomaio, Nov 15, 2005.

  1. #1
    I am killing myself here, i've managed to create a WhoIs function for a website to see if it is registered or not. It works great, but I need to use that information in three diferent places.

    Is it possible to access the variable content that the function generates outside the function?

    Let me give you an example, this is the function:

    
    <?php
    global $domain_status;
    function whois ($server, $query){
    //The function does is thing and gives the results...
    if (eregi($available,$result)) {$domain_status = "available";}
    else {$domain_status = "notavailable";}
    }
    ?>
    
    PHP:
    I call the function in some place of my page with whois($server, $query) and if the domain is available the $domain_status will give available and so on...

    Now if in somewere of my page I add this code:

    <?php
    echo($domain_status);
    ?>
    PHP:
    It will give me nothing, blank! :(

    Can someone please help me on this? I need the result of that function available in 3 diferent places and I can't run the function 3 times.
     
    eduardomaio, Nov 15, 2005 IP
  2. eduardomaio

    eduardomaio Peon

    Messages:
    450
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Oh, silly me... I've solved it... I've just needed to add $GLOBALS['domain_status'] and it works...

    Maybe this will be useful for someone else. Thanks anyway guys!
     
    eduardomaio, Nov 15, 2005 IP
  3. hnn

    hnn Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You should have the global statement inside the function...

    
    <?php
    function whois ($server, $query)
    {
       global $domain_status;
       //The function does is thing and gives the results...
       if (eregi($available,$result))
       {
           $domain_status = "available";
       }
       else
       {
           $domain_status = "notavailable";
       }
    }
    ?> 
    
    PHP:
     
    hnn, Nov 15, 2005 IP