Reputation: 1383
Is it possible to have just normal php code and not use eval()
function in such case?
My eval code is following:
eval('$'.$var.'["'.$key.'"]="'.$value.'";');
And what I am doing is following:
function config_update($array, $var="result")
{
global $result, $dbi;
while (list($key,$value)=each($array))
{
if (is_array($value))
{
config_update($value, $var.'["'.$key.'"]');
}
else
{
for ($i=0; $i<count($value);$i++)
{
if ($value == "{FROM_DB}")
{
$query = $dbi->prepare("SELECT `value` FROM `".PRE."config` WHERE `key`=?;")->execute($key)->results();
if (!$query)
{
trigger_error("There is no such key as `{$key}` in config database",E_USER_ERROR);
exit;
}
else
$value = str_replace("{FROM_DB}",$query[0]['value'],$value);
}
eval('$'.$var.'["'.$key.'"]="'.$value.'";');
//$$var[$key] = $value; #smth like that..
}
}
}
}
I basically need to update variable values with {FROM_DB} with values coming from DB.
Upvotes: 0
Views: 148
Reputation: 456
from the php manual on variable variables you should use {} when working with array based variable variables to avoid unknown behavior.
${$var}[$key] = $value
Upvotes: 2
Reputation: 11588
If you were to use:
$$var = array();
$$var[$key] = $value;
It would evaluate as:
$result[$key] = $value;
Assuming that $var = 'result'
.
Upvotes: 1