Quick PHP question

Discussion in 'PHP' started by MCJim, Jun 10, 2008.

  1. #1
    I'm storing an operator, as in '+' or '-', in a variable. For example '$operator = "-"'. I would like to do something like this:

    $operator = "-";
    $numone = 5;
    $numtwo = 5;
    $answer = $numone $operator $numtwo;
    PHP:
    Obviously this doesn't work, but I would like to know how it could. Thanks in advance!
     
    MCJim, Jun 10, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try:

    
    $answer = $numone.$operator.$numtwo;
    
    PHP:
     
    jestep, Jun 10, 2008 IP
  3. hanushh

    hanushh Peon

    Messages:
    198
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Also add
    echo "<br /> Answer is ".$answer;
    PHP:
    to display the answer
     
    hanushh, Jun 10, 2008 IP
  4. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It displays it properly but it doesn't subtract it. So, in this example

    $operator = "-";
    $numone = 5;
    $numtwo = 5;
    $answer = $numone.$operator.$numtwo;
    echo $answer;
    PHP:
    it echos '5 - 5' instead of '0'. I need it to read "-" as an operator.
     
    MCJim, Jun 10, 2008 IP
  5. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #5
    $operator = "-";
    $numone = 5;
    $numtwo = 5;
    $answer = eval("return($numone $operator $numtwo);");
    echo $answer;
     
    shallowink, Jun 10, 2008 IP
  6. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #6
    This Works.
    REMOVED :D
    ?>
    PHP:
     
    rohan_shenoy, Jun 10, 2008 IP
  7. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK thanks, I got it working.
     
    MCJim, Jun 10, 2008 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    Works if the operator is - or +. eval handles * and / as well.
     
    shallowink, Jun 10, 2008 IP
  9. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #9
    ^^Hmm........I forgot that.......plz don't use my method, use Shallowink's :)
     
    rohan_shenoy, Jun 10, 2008 IP
  10. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #10
    I would probably lean towards eval since it will handle any mathematical operator (%^/+-* etc...).

    If you don't need every function, I would definitely not use eval, because it is potentially a resource heavy function and it can be a security concern.
     
    jestep, Jun 10, 2008 IP
  11. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #11
    I have always heard eval was risky and resource hungry, but is there another way to do that without using eval?
    rohan_shenoy 's method would work if you knew the operations but eval would be better. Less code etc.
     
    shallowink, Jun 10, 2008 IP