JavaScript runs on the client side while PHP runs on the server. They execute at completely different times in the page lifecycle, so they cannot communicate in the manner you have used.
Instead you could to use AJAX to send a value in JS to a PHP page.