From PM:
But I'm having issues breaking down exactly what happens within the "while" in this example.
Could I possibly get you to do me the favour of telling me in exact detail what transpires in there?
Let us analyse this code snippet:
$query = mysql_query("SELECT name, param FROM parameter
WHERE name='siteuser' OR name='siteuserpasswd'")
OR die(mysql_error());
while($row = mysql_fetch_array($query))
{
$$row['name'] = $row['param'];
}
echo $siteuser . '<br />' . $siteuserpasswd;
On the first iteration of the while loop (or the second, it does not matter), $row['name'] has the value 'siteuser'. So, this can be substituted into $$row['name'] such that it becomes $siteuser. Thus,
$$row['name'] = $row['param'];
becomes:
$siteuser = $row['param'];
On the next iteration, $row['name'] has the value 'siteuserpasswd'. Likewise, this can be substituted into $$row['name'] such that it becomes $siteuserpasswd. As such,
$$row['name'] = $row['param'];
becomes:
$siteuserpasswd = $row['param'];
This is how the last line in the code snippet can use $siteuser and $siteuserpasswd even though they do not seem to exist anywhere at a glance.
I have the feeling that this is quite a usable concept, so perhaps it's a good idea of picking it up early and I can't relate the basic descriptions found on the net to explain my particular example, and focusing on my particular example might just be what it takes for me to pick this concept up once and for all.
I rarely use variable variables, personally. The reason is that very often people use variable variables where an array will do, e.g.,
for ($i = 0; $i < 10; ++$i)
{
${'numbers' . $i} = $i;
}
echo $numbers7;
which could be better written as:
$numbers = array();
for ($i = 0; $i < 10; ++$i)
{
$numbers[$i] = $i;
}
echo $numbers[7];
Or, for this example, even as concisely as:
$numbers = range(0, 9);
echo $numbers[7];
Any recomendable tutorials on the practical use of variable variables?
I do not know of practical tutorials, though the PHP Manual has a short section on variable variables.