note 29020 added to function.eval
| From: | Taras at rack1 dot php dot net | Date: | Thu, 30 Jan 2003 16:35:34 +0000 |
| Subject: | note 29020 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43136@lists.php.net to get a copy of this message | ||
A quick and simple (and hopefully safe) method of evaluating sums:
function evalsum($sum){
// Sum may only contain the following characters:
$check = strtr($sum, "0123456789+-*/()", " ");
if ( trim($check) == "" ) {
$to_eval="\$evalled=" . $sum . ";";
eval($to_eval);
}else{
$evalled = "Evaluation error";
}
return $evalled;
}
echo evalsum("2+2");
Unfortunately, since PHP uses pow(a,b) for powers rather than a^b, this won't do powers.
--
http://www.php.net/manual/en/function.eval.php
http://master.php.net/manage/user-notes.php?action=edit+29020
http://master.php.net/manage/user-notes.php?action=delete+29020
http://master.php.net/manage/user-notes.php?action=reject+29020