Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    16
    Thanks
    0
    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.

    Thanks!
    Ad Infinitium


  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    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:
    PHP Code:
    list($lhs$op$rhs) = sscanf($input"%d%s%d");
    $op trim($op);
    $result 0;
    switch (
    $op)
    {
        case 
    '+':
            
    $result $lhs $rhs;
            break;
        case 
    '-':
            
    $result $lhs $rhs;
            break;
        case 
    '*':
        case 
    'x':
            
    $result $lhs $rhs;
            break;
        case 
    '/':
            
    $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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •