Taking information from a function

Asked

Viewed 70 times

-1

I want to save the geographical coordinates of an address in the database. I can do this, but there is a.

I have a function that takes an address and returns the geographical coordinates of it. The problem is that as the function has an "echo" to display the coordinates and it is locking my application. Do you know how to make this function return the result without being through "echo" or do you know how to make the application not "stop" when going through "echo"?

Follows the function:

function getCoordinates($address){

    $address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern

    $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";

    $response = file_get_contents($url);

    $json = json_decode($response,TRUE); //generate array object from the response from the web

    return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);

    }

    echo getCoordinates($address);
  • Takes the echo. Other than that you can’t understand what you want.

  • Gustavo, You can add other details to the question?

  • @bigown, I simply want the result, the answer the function information, ie the coordinates, but echo stops my application... I’m using the function inside an application of mine!

  • @Mega, what other details do you need? .

2 answers

1

If this is file called multiple times, leaves only this function in the file and echo in the other files if it is not possible, turn on the output buffer to avoid the error of aready headers sent with ob_start();

ob_start();
echo getCoordinates($address);
  • Not if that’s what he wanted, but the answer is correct in the context he used ;)

  • Sorry @rray, I don’t understand anything you meant.

  • the screen is blank, @Gustavosevero?

  • Not @rray, only the coordinates appear and does not go to the other page as it was due, because I put a header("Location") at the end.

  • @gustavosevero, did ob_start() work? You need to imprint the coordinates on the screen or just redirect them to another page?

-1

If you just want to thresh your code to know what your function is returning, use the following command:

echo '<pre>';
var_dump(getCoordinates($address));
echo '</pre>';

reference

  • I know what the function is returning, that’s not the case.

  • 1

    Guys, I did it! I just took echo! Thanks anyway for the tips.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.