Solved

# PHP Calculator

Posted on 2004-11-02
324 Views
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
Question by:harveykane

LVL 15

Accepted Solution

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;");

Cheers,
Matt
0

LVL 20

Expert Comment

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;

0

LVL 8

Expert Comment

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

LVL 8

Expert Comment

oops, my refresh didn't work.  Two people posted already.  Should've typed faster. :)
0

LVL 2

Author Comment

Thanks everyone. That answer was a lot simpler than I was expecting :) All good.
0

LVL 7

Expert Comment

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

LVL 2

Author Comment

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

LVL 15

Expert Comment

The escapeshellarg() and escapeshellcmd() functions can be useful for this type of thing, too ;)
0

## Featured Post

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.