how to hold javascript variable in php variable

I want to use javascript variable as php variable. I am echo php variable then its print. but when i am use for fetching data from database its show an error
Notice: Undefined index: document.write(i)

here my code

javascript

var i=0;
function inc()
{
    i+=1;
}
<?php $foo="document.write(i)"; ?>

php

code work for

echo $foo

code not work for

$i=$foo;
$query="select * from TABLE where id = $i";
$result=mysqli_query($conn,$query);
while($row=mysqli_fetch_row($result))
    {
        echo $row[0];
    }

Then It show This Error Notice: Undefined index: document.write(i)


Source: stackoverflow-php

How do I call for a certain variable or another depending on a string I have

I have two variables, varL and varR, which have the information that they are L and R respectively. Depending on wether a third object has as imput ‘L’ or ‘R’, I would like to reference varL or varR respectively. Is there any way to do this?

Specifically, varL and varR are arrays. If the third object has as imput ‘L’, I would like to have access to varL[i] for some i, and the same goes for ‘R’. I know this is not even remotely correct, but I was looking for something like

(var +’L’)[i] or something


Source: stackoverflow-javascript