PHP Calculator

I need a PHP calculator function to calculate values from a string variable.

It needs to take a string as input eg.

$input = "(1+4)*3";

Feed this value into the function and then output the answer (eg 15).

It needs to be able to deal with addition, subtraction, multiplication, division, decimals and brackets, as well as applying the correct mathematical order of operations.

If someone has a function that does this, great! Otherwise I can write the function myself if someone can point me in the right direction, and give me some ideas on the logic to take with this puzzle.

Any help appreciated :)
LVL 2
harveykaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nicholassolutionsCommented:
Here is a simple way to do it. The idea is to store the desired calculation in a string, and then evaluate the string as PHP code, thus taking advantage of PHP's built-in mathematical functionality:


$a= '3*(5-1)/3';                // = 3*(4)/3 = 4
eval("\$b = $a;");
echo "the answer is $b";   // outputs: "the answer is 4"


Cheers,
Matt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
virmaiorCommented:
you could eval()
http://us2.php.net/manual/en/function.eval.php

just have your program procuce php code..

$runthis = "$myoutputvar = " . "(1+4)*3";
eval($runthis);
echo $myoutputvar;

TADA!
0
inq123Commented:
Hi harveykane,

It's easy, here's my code that'll do it all for you :)

<?
  if($_REQUEST['sub'])
  {
    eval("\$test = $_REQUEST[exp];");
    print("result is: $test");
  }
  else
  {
?>
    <form>
      <input type=text name=exp>
      <input type=submit name=sub value=calculate>
    </form>
<?
  }
?>

Cheers!
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

inq123Commented:
oops, my refresh didn't work.  Two people posted already.  Should've typed faster. :)
0
harveykaneAuthor Commented:
Thanks everyone. That answer was a lot simpler than I was expecting :) All good.
0
jdpipeCommented:
You need to be really careful with code like that. Make sure there's no risk of people running malicious code like the follow, via your 'eval' command.

`cat /etc/passwd`

I suggest that there are heaps of regexps you need to apply to make sure that there are no funny things getting through into your 'eval' function...

JP
0
harveykaneAuthor Commented:
Lol, yeah, I wouldn't ever use this in a form that the user can type into.

It's only ever going to be run from internal string variables and database output.
0
nicholassolutionsCommented:
The escapeshellarg() and escapeshellcmd() functions can be useful for this type of thing, too ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.