Dynamic variable names

from the Artful MySQL Tips List


PHP offers a simple trick for data-driven code, supporting runtime naming of variables: Given ...
$varname = 'firstname'; 
... then prepending an extra dollar sign to the variable name ...
$$varname = 'julie'; 
... creates a variable by that name and assigns the indicated value to it. Then ...
echo $varname, " is ", $$varname, " i.e, ", $firstname; 
firstname is julie i.e, julie

Last updated 3 Nov 2015


Return to the Artful MySQL Tips page