In addition, it is possible to use associative array to secure name of variables available to be used within a function (or class / not tested).
This way the variable variable feature is useful to validate variables; define, output and manage only within the function that receives as parameter
an associative array :
array('index'=>'value','index'=>'value');
index = reference to variable to be used within function
value = name of the variable to be used within function
<?php
$vars = ['id'=>'user_id','email'=>'user_email'];
validateVarsFunction($vars);
function validateVarsFunction($vars){
//$vars['id']=34; <- does not work
// define allowed variables
$user_id=21;
$user_email='[email protected]';
echo $vars['id']; // prints name of variable: user_id
echo ${$vars['id']}; // prints 21
echo 'Email: '.${$vars['email']}; // print [email protected]
// we don't have the name of the variables before declaring them inside the function
}
?>