I was wondering if there was a way to have an automatic variable replacement inside a string. I can simulate this right now with preg_replace and strtr but am not sure if there's a better way. I was thinking of using eval, but can't seem to figure out how to do it properly.
strtr
<?php
$replacement = array('$test' => "dog");
$template = 'this is a $test';
$statement = strtr($template, $replacement);
echo $statement;
?>
preg_replace
<?php
$template = 'this is a $test';
$statement = preg_replace('/\$test/', 'dog', $template);
echo $statement;
?>
eval idea
<?php
$template = 'this is a $test';
$test = 'dog';
eval('$statement = "$template";');
echo $statement;
?>
eval().eval()may make hacking your site much simpler for clever, naughty people. It is not against the law to useeval()but it is often strenuously discouraged due to vulnerabilities. I would urge you to makeeval()your plan Z -- only to be used when everything else fails to perform as you require.evalshould be safe since they'll be variables I'm defining myself. Not user/client side input.