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
https://forums.phpfreaks.com/topic/67098-solved-return-feature-in-php/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.