Click to See Complete Forum and Search --> : php calculator script


gameguy43
02-26-2005, 06:57 PM
hey, i just recently ventured into the scary yet rewarding world of php. So far all i really do is use ALOT of includes, and I learned how to save stuff to a file, so i made this page that has a basic forum-type thing. Anyway, something that I really want to do is make an online calculator program, and im afraid that this might require the use of some hard-core functions that do stuff with strings. I looked at the php.net manual and there are just too many functions to sort through. Could someone perhaps just push me in the right direction here, gimme some basic script that I can build off or just some functions that they recommend me using? Your help is much appreciated!

the tree
02-27-2005, 05:42 AM
Well for starters:$a = $b + $c; // Changes the value of $a to the sum of $b and $c
$a = $b / $c; // Changes the value of $a to $b diveded by $c
$a = $b * $c; // Changes the value of $a to $b multiplied by $c
$a = $b - $c; // Try guessing this one
From your post I guess you know how to use POST, although GET would be just as good for this purpose.
To give the awnsers, you want to use echo or print (which do exactly the same thing).

patenaudemat
02-27-2005, 01:02 PM
If you're writing a calculator script, you'll find the eval() function to be pretty useful; throw in any combination of integers, strings, etc. and it'll treat it as a PHP statement. For example, user enters 5+7-6 into form and submits it, method POST, field name calc:

<?php
$calc = $_POST['calc']; // assigns form value to $calc

$result = eval($calc); // executes 5+7-6 and stores to $result

echo $result; // prints the result (6)
?>

Of course, you'd probably want to get a little fancier than that, but that'll do the trick for basic operations.

gameguy43
02-27-2005, 02:17 PM
oh my god, that eval function is amazing, just what i needed! thank you so much. I thought that there was probably a simpler way to do it. Thank you so much, you have saved me a ridiculous amount of time. Thanks also to that other dood, but i'm not THAT much of a nub...

patenaudemat
02-27-2005, 02:57 PM
Glad I could help! :D

gameguy43
02-27-2005, 03:39 PM
didnt quite work, i looked up your eval funtion on php.net, and it turns out that its a really tricky function. I played around with it alot, and i cam out with the following code:


$calc = $_POST['calc'];
eval("\$calc = \"$calc\";");
echo($calc);


when i punch in an equation, it just spits it back out as a string... Well, at least now im not getting error messages. Almost there, anyone else have any suggestions?

patenaudemat
02-27-2005, 03:56 PM
Oops, posted a little too quickly... I got this to work:

<?php
$calc = $_POST['calc'];
$result = eval('echo '.$calc.';');
echo $result;
?>

I tried this with GET instead of POST, and it gave me problems when I tried to add... I don't know if that will happen with POST...

In addition, I neglected to think of security when using this... I'm not sure, but I believe it's possible to inject some nasty code into this type of page. Be careful how you use it, I'll post if I think of a safer way.

gameguy43
02-27-2005, 04:57 PM
works now, thanks again. it freaks out if u start putting in html tags and stuff, so i think that there isnt really a concern for security. Now what i want to do is make it show a history of things solved, but without saving it to a file. I'd also like to set this up so that it doesn't reload the whole page after a problem. I think this can be done with frames. Anyhu, thanks for your help, if anyone has some suggestions that come to mind immmediately about the above things, please tell me, if not then i'll just keep playing around with it.

patenaudemat
02-27-2005, 05:11 PM
For a history, I'd suggest just using a variable that was stored in a hidden field in the form. Call it $history, for example, and on load have it do this:

$history = $_POST['history'];

/* Calculation code here, intial equation as $calc, result as $result */

$history .= "<BR>".$calc." = ".$result;

And then in the form:

<INPUT TYPE="hidden" NAME="history" VALUE="<?php echo $history; ?>">

Hope something like that works for you!