It's easy to pass PHP variables to JavaScript but there's no direct way to pass JavaScript variables to a PHP script - but there is an indirect way.
Passing PHP Variables to JavaScript
Since PHP can output plain text, HTML or JavaScript all you need to do is use echo() to print out a string, with PHP variables embedded, inside a JavaScript.
Since JavaScript can output HTML using the document.write() method you can construct a link using concatenation to write the JavaScript variables into the query string.
When a user clicks the link the JavaScript variables will be passed to the PHP script addressed by the link.
Passing JavaScript variables to a PHP Script
<html>
<head>
</head>
<body>
<script language="javascript" type="text/javascript">
var name = "Dave";
var age = 40;
document.write('<a href="another_page.php?name='+name+'&age='+age+'">another_page</a>');
</script>
</body>
</html>
Important Points to Note
Query strings can produce unforeseen results if certain characters are used i.e. characters that are reserved as delimiters.
So if you're not sure what value a string variable may take then you should have code in place to check it before it's passed, especially if the string is gathered from user input.
You can't use this method to pass JavaScript variables to a PHP script on the same page as the JavaScript. They have to be passed to a PHP script on another page.
The reason is that PHP is parsed server side before the page is downloaded and JavaScript is parsed client side after the page is downloaded.
The variable values have to be written before they're passed and this wouldn't happen if you tried to write them directly to PHP in the same page, since the PHP would be parsed before the JavaScript.