0

Script taken from http://css-tricks.com/snippets/php/get-geo-ip-information/

    .
    .                   
    .
             //Array where results will be stored
                       $ipInfo=array();

                       //check response from ipserver for above patterns
                       foreach ($patterns as $key => $pattern)
                       {
                               //store the result in array
                               $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
                       }


                      return $ipInfo;
         }

This displays the $ipInfo.

The thing is that i don't want it to display $ipInfo. I want to save the output in a variable and then echo it later, when i need it in another place. How can i do that?

3
  • 2
    There is no outputting in this script. Wherever this function is called there must be an echo or print_r. Commented Sep 7, 2013 at 23:54
  • Your above code already returns a variable. You just need to pass its result around. Commented Sep 7, 2013 at 23:55
  • @Kolink Yes, your suggestion is right. Now i feel so dumb that i didn't see it. Commented Sep 7, 2013 at 23:57

1 Answer 1

4
print_r(geoCheckIP($ip));

This is what's doing the outputting. Just assign to a variable instead:

$somevar = geoCheckIP($ip);

This is trivial PHP that you really should know.

Sign up to request clarification or add additional context in comments.

1 Comment

You could also set it to a global variable within this function and than reinitialize as global variable in any other function that gets called, and get it that way instead of returning the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.