[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

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 :)
0
harveykane
Asked:
harveykane
  • 2
  • 2
  • 2
  • +2
1 Solution
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now