Jaymin
Jaymin

Reputation: 1661

A non well formed numeric value encountered using maths functions in cakephp3

I have seen all past examples of this error but the examples are for a date function. My problem is different because I am using maths function and it throws me this error:

A non well formed numeric value encountered on line 257

Below is my code snippet - I am doing my code on cakephp3.x

foreach($query as $row)
{
    $lat=$row->lat.'<br>';
    $lng=$row->lng.'<br>';
    $lat1=$formlatitude;
    $lon1=$formlongitude;
    $lat2=$lat;
    $lon2=$lng;

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);
    $miles=$miles * 1.609344;
    //echo $miles;
    //Although distance is calculated in kms only
    if($miles<10)
    {
        echo $miles;
    }

}

I have values of variables like

$lat1=23.02650164397716

And so on - they are decimals. The error occurs on this line:

$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));

Upvotes: 1

Views: 936

Answers (1)

moopet
moopet

Reputation: 6175

You are setting

$lat=$row->lat.'<br>';

followed by:

$lat2=$lat;

So now regardless of what $lat1 contains, $lat2 is a string with HTML in it, not a number.

Next up, you try to use $lat2 as a number in:

deg2rad($lat2)

It'll probably work if you use:

$lat2=$row->lat;

instead.

I think the problem you're facing could be better addressed by making sure you know what's in each variable. Using a name like lat to refer to a string in one place and a number in another is likely to cause issues as time goes on.

Upvotes: 1

Related Questions