Hi I have a problem
I am writing a voucher script for my website. I need to perform a maths equation on a string. e.g
$var1 = "/2"; (string)
$var2 = "20"; (int)
I need to perform the string function on the int to make $amount = (20 / 2); I could have various strings e.g $var could be "-10"; or "*3"
then store that as a variable.
Am i making sense?
maths from string?
Moderator: General Moderators
Perhaps use eval()?
- php3ch0
- Forum Contributor
- Posts: 212
- Joined: Sun Nov 13, 2005 7:35 am
- Location: Folkestone, Kent, UK
i have tried
and get a parse error. Am I on the right track?
Code: Select all
$amount = "20";
maths ="/2";
$amount = eval($amount."".$maths);Code: Select all
$amount = "20";
$maths ="/2";
$equation = "\$answer=".$amount.$maths.";";
eval($equation);
echo $answer;I would strongly recommend against eval... I believe there's a Math_RPN package in pear...
Code: Select all
$rpn = new Math_Rpn();
echo $rpn->calculate($expression,'deg',false);