passing JavaScript variable into a php for loop not working [duplicate]

I am trying to limit a PHP loop based on a variable passed from JavaScript into the for loop as follows, however it doesn’t seem to be working.

</script>
var res =  5;
</script>

<?php
   $xx = "<script>document.writeln(res);</script>";
   echo $xx. " the number echo's fine";
?>

<br>

<?php  
for ($i= 1; $i <= $xx; $i++) {
  echo "The number is: $xx <br>";
}
?>  

Help with a solution would be greatly appreciated and thanks in advance!
I am new to PHP.

Aran