Let's say I have some variables declared - but I don't know exactly which, I just have an array with variable names.
$variable_list = array('var1', 'var2', 'var3', 'var4');
We go ahead and assign some values.
foreach($variable_list as $var_name){
$$var_name = rand(100,1000);
}
Now I want to unset these variables in a similar fashion. Not remove them from list, but unset the ACTUAL variable.
foreach($variable_list as $var_name){
unset($var_name);
}
this does not work. any ideas?
foreach($variable_list as $var_name){
unset($$var_name);
}
Why do you set them using variable variables ($$var_name
) and don't unset them like that? This should work:
foreach($variable_list as $var_name){
unset($$var_name);
}
However, since you say:
Now I want to [...] not remove them from list, but unset the ACTUAL variable.
Simply use:
foreach($variable_list as $var_name){
$$var_name = null;
}