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
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.
<?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):
<?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 ...
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.
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 ?
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.
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.
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...
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/