Follow the code
Follow the code
You will only be able to pass this data via Ajax.
When PHP renders the page it literally writes that $teste = "<script>getLocation()</script>";
. For it "<script>getLocation()</script>"
is simply a string. It has no meaning and does nothing.
Who runs Javascript is the browser after pure HTML generated by PHP is ready and rendered to the end user. At that time PHP has no more power on the page.
What you can do is create a function that runs this command and send the result via AJAX to your PHP project.
AJAX is separated into two steps. Sending the browser made with Javascript you can read more about in the stackoverflow itself: Ajax request with pure Javascript (without APIs)
And the API in PHP that will receive this request. Here's a little tutorial talking about it too: link
It is not a trivial process. You will need to study a little to understand how to perform because they are called asynchronous, but it is the only way to get the user's location through the Browser API.
First remove the location of the function and leave it like this:
return position.coords.latitude+","+ position.coords.longitude;
Then:
$teste = "<script>getLocation()</script>";
echo $teste;
$latAqui = explode(",", $teste);
echo($latAqui[0]);
echo($latAqui[1]);