 <p>You can write in this way </p> <pre><code> &lt;script&gt; var x = -40; var y = 72; document.cookie = 'latitude' + "=" + x; document.cookie = 'longitude' + "=" + y; &lt;/script&gt; &lt;?php if(isset($_COOKIE['latitude']) &amp;&amp; isset($_COOKIE['longitude'])) { echo $_COOKIE['latitude']; echo $_COOKIE['longitude']; }else{ ?&gt; &lt;script type="text/javascript"&gt; document.body.innerHTML = x +'&lt;br&gt;'+y; // or in some element as you like &lt;/script&gt; &lt;?php } ?&gt; &lt;/body&gt; </code></pre>