how to return Javascript variable in JSON format PHP? : <p>I want to <strong>return</strong> Javascript variable in JSON format in PHP.</p>

<p>Below is my Javascript code which returns Longitude and Latitude.</p>

<pre><code>function showPosition(){
        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(function(position){

                 var positionInfo = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
                //document.getElementById("result").innerHTML = positionInfo;


            });
        } else{
            alert("Sorry, your browser does not support HTML5 geolocation.");
        }
    }
    showPosition();
    //document.write(positionInfo);
</code></pre>

<blockquote>
  <p>How can i get the values of variable <code>position.coords.latitude</code> and <code>position.coords.longitude</code> as response in json format using php</p>
</blockquote>

<p><strong>Please note:</strong> We are calling this php script in background, so solution of fetching the variables on html input fields is not a viable solution for us.</p>
