Jump to content

[SOLVED] return feature in PHP


ainoy31

Recommended Posts

Hello-

 

I have an issue with returning data to my calling function.  Please let me know if I am overseeing something dumb.

 

Code:

function x

{

    $this->Mileage($start, $end);

 

    $display  = sprintf("%0.2f",$miles).' miles';

    echo "The total driving distance is: " . $display;

 

}

 

function Mileage($start, $end)

{

echo "<Starting Coordinates> \n";

echo "Latitude: ". $start[0]['lat'] . " Longitude: ". $start[0]['long'];

echo "<br>";

echo "<Ending Coordinates> \n";

echo "Latitude: ". $end[0]['lat'] . " Longitude: ". $end[0]['long'];

 

$lat1 = $start[0]['lat'];

$long1 = $start[0]['long'];

 

 

$lat2 = $end[0]['lat'];

$long2 = $end[0]['long'];

echo "<br>";

 

 

// Formula for calculating distances from latitude and longitude.

    $dist  = acos(sin(deg2rad($lat1))* sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($long1 - $long2)));

 

    $dist  = rad2deg($dist);

    return ($miles  = (float) $dist * 90.90);

}

Link to comment
Share on other sites

Just with a quick glance, it looks to me like you're mising a couple of things. Firstly, when you call your Mileage function, you pass in two variables: $start and $end. Unless these are global variables, they are undefined within your function, x.

 

Second, if you're function returns a value, you need to do something with the returned value, its no use just making a call to the function. Generally, you would either assign the returned value to a variable, or echo the returned value.

 

Edit: Beaten to it, but thought id post anyway, what with the additional information.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.