Bad function in calc operator

Discussion in 'PHP' started by <|>, Apr 3, 2010.

  1. #1
    Hy guys

    I look more that one hour and i dont find where is the problem in my calculation operator code. I have problem with -, / and * but not with +. For example:

    echo calc(21,7,"+");
    PHP:
    give me 28 is ok. But with "-" function give me -7 or with function "/" give me 0.

    This is the code:

    <?php
    
    function calc($numa1, $num2, $op)
    {
    
     switch ($op)
     {
       case "+":
       $total = $numa1 + $num2;
       return $total;
       break;
    
       case "-":
       $total = $num1 - $num2;
       return $total;
       break;
    
       case "*":
       $total = $num1 * $num2;
       return $total;
       break;
    
       case "/":
       $total = $num1 / $num2;
       return $total;
       break;
    
       default:
       echo "Unknown operator.";
    
     }
    
    
    }
    
    echo calc(21,7,"+");
    
    ?>
    PHP:

    If you se everything is good and, but is working only "+" function and the another not :confused:
     
    <|>, Apr 3, 2010 IP
  2. l3vi

    l3vi Peon

    Messages:
    375
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You have a typo. your plus has "numa1", while your function calls for "numa1", everything else has "num1". So when you minus 7 from what you think is 21, your subtracting 7 from 0 which is -7.
     
    l3vi, Apr 3, 2010 IP
  3. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    
    function calc($num1, $num2, $op)
    {
    
     switch ($op)
     {
       case "+":
       $total = $num1 + $num2;
       return $total;
       break;
    
       case "-":
       $total = $num1 - $num2;
       return $total;
       break;
    
       case "*":
       $total = $num1 * $num2;
       return $total;
       break;
    
       case "/":
       $total = $num1 / $num2;
       return $total;
       break;
    
       default:
       echo "Unknown operator.";
    
     }
    
    
    }
    
    echo calc(21,7,"+");
    
    ?>
    
    Code (markup):
     
    Brandon.Add.On, Apr 3, 2010 IP
  4. aTo

    aTo Active Member

    Messages:
    473
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    78
    #4
    <?php
    
    function calc($num1, $num2, $op)
    {
    
     switch ($op)
     {
       case "+":
       return  $num1 + $num2;
       break;
    
       case "-":
       return  $num1 - $num2;
       break;
    
       case "*":
       return  $num1 * $num2;
       break;
    
       case "/":
       return  $num1/ $num2;
       break;
    
       default:
       echo "Unknown operator.";
    
     }
    
    
    }
    
    echo calc(21,7,"+");
    
    ?>
    PHP:
    guess that's the correct way for it... and that's the shortest way :)...
     
    aTo, Apr 3, 2010 IP
  5. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You could even take it one step further and use func_get_args() to get any number of numbers so you aren't just limited to 2 numbers and 1 operator. :)
     
    Brandon.Add.On, Apr 3, 2010 IP
  6. <|>

    <|> Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks guys is working :)

    Can you explain a litlle bit how you got that conclusion i mean cut this line
    return $total;
    PHP:
    I started to learn PHP for one week ago .., so I don't know what it is, can you explain me a litlle with a demo code ?
     
    <|>, Apr 3, 2010 IP
  7. l3vi

    l3vi Peon

    Messages:
    375
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can learn about any part of PHP at php.net. For func_get_args see: http://php.net/manual/en/function.func-get-args.php

    func_get_args allow you to construct a method also known as a function depending on usage that has no required input values, then pass arguments(args) when you call the method when it's constructed or used. I recommend you do not make this a habit as it will make it harder to know what a method does later on when you have to use it again or edit it, and services like PHP Documentor with not document the method when you do that.
     
    l3vi, Apr 3, 2010 IP
  8. l3vi

    l3vi Peon

    Messages:
    375
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #8
    And for fun.... Something that does the same thing that is a little more advanced, but not to complicated to understand how it works.

    
    <?
    function calc($sMath) {
    	$doCalc = create_function("", "return (" . $sMath . ");" );
    	return $doCalc();
    }
    print calc("3+3");
    ?>
    
    PHP:
    We could push this process down to a one liner, but that would get pointless after a while. The method above create another method with create_function from the values you passed into it, this doing whatever math you want from a string.
     
    l3vi, Apr 3, 2010 IP
  9. aTo

    aTo Active Member

    Messages:
    473
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    78
    #9

    well, u can lessen the use of ur variables for that, returning $total is just the same as returning the operation u r doing :) but, there is nothing wrong with the first code(except for the variable $numa), i just made it efficient, :) so u got it? np with that... :)
     
    aTo, Apr 3, 2010 IP
  10. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Perfection [in design] is achieved, not when there is nothing more to add, but when there is nothing left to take away.

    One of my fav all time programming quotes/
     
    Brandon.Add.On, Apr 3, 2010 IP