Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
10-09-2009, 11:27 PM #1
- Join Date
- Jul 2007
- Thanked 0 Times in 0 Posts
How to have mathematic operators in a submit form?
Hey there, I'm clueless about how to do this and I was thinking how could you add support of mathematic operators in a submit form?
Kind of like Google, where you can type in 2*2 or 2x2 in the search box and it gives you an answer.
10-10-2009, 02:26 AM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,662 Times in 2,631 Posts
Use sscanf to format you're input and then perform the operation. You'll need to choose a format for it to be of any use though:
You'll need some error checking. You won't need to atoi this at all since php can interpret strings as numbers.PHP Code:
list($lhs, $op, $rhs) = sscanf($input, "%d%s%d");
$op = trim($op);
$result = 0;
$result = $lhs + $rhs;
$result = $lhs - $rhs;
$result = $lhs * $rhs;
$result = $lhs / $rhs;
Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)PHP Code:
header('HTTP/1.1 420 Enhance Your Calm');