View Full Version : How to have mathematic operators in a submit form?
10-10-2009, 12:27 AM
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, 03:26 AM
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:
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;
You'll need some error checking. You won't need to atoi this at all since php can interpret strings as numbers.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.