0

I'm trying to make a calculator in PHP now I have written a function in php to add few numbers but its concatenate instead of adding it can someone please help me guide me to do the task.

function cal($val1 , $opt , $val2){
  return $sum =  $val1 . $opt . $val2;
}

Now, if I'm calling this function like:

echo cal(4,"+",5);

It returns 4+5 but I'm looking 9 for an answer can it be possible that $opt works as an operator instead of a string and i can add or subtract based on operators like

jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
usman
  • 15
  • 4
  • well, `.` IS the concatenation operator... so how should it do anything *but* concatenate? – Franz Gleichmann Oct 19 '16 at 10:43
  • You should use `eval()` – Mohammad Oct 19 '16 at 10:44
  • 3
    @Mohammad you should *never* use `eval()` unless there is absolutely no other way. in this case, there is. looking at what the operator is and implementing the function. – Franz Gleichmann Oct 19 '16 at 10:44
  • @FranzGleichmann No, it is safe if you checked parameter, – Mohammad Oct 19 '16 at 10:45
  • 1
    @Mohammad concidering the question, i'm pretty sure the parameters aren't checked. – Franz Gleichmann Oct 19 '16 at 10:46
  • 1
    @Mohammad The issue with statements like "it is safe if you checked parameter" is that you have to be absolutely certainly that you think of all possible things that might occur. Is that _really_ realistic? I agree that `eval()` typically is to be avoided at all costs. – arkascha Oct 19 '16 at 10:47
  • its just a prototype its working fine i ve completed it with switch but i wanted to concise it make it more dynamic we can make a string int or float or any other thing why not making it an operator ? – usman Oct 19 '16 at 10:48
  • 4
    Possible duplicate of [PHP use string as operator](http://stackoverflow.com/questions/5780478/php-use-string-as-operator) – zaidysf Oct 19 '16 at 10:49
  • @mohammad eval() is one good way of doing thanks for suggestion is it any other day – usman Oct 19 '16 at 10:52
  • Check answer of @Zaid Yasyaf http://stackoverflow.com/questions/40129102/can-we-convert-a-string-into-a-sign-in-php#40129229 – Mohammad Oct 19 '16 at 10:58
  • Mark an answer as correct since there are solutions to you question so other people don't have to make the same question again. – Zenel Rrushi Nov 10 '16 at 21:25

4 Answers4

1

You should do an if statement or a switch case one;

example

if ($opt =='+'){
   return $val1+$val2;
}else if($opt =='-'){ 
   return $val1-$val2;
} .... etc
Zenel Rrushi
  • 2,346
  • 1
  • 18
  • 34
0

You can use eval() run string as code. You should check values before using it.

function cal($val1 , $opt , $val2){
    if (is_numeric($val1, $val2) && preg_match("/[+-\/\*%]/", $opt))
        return eval($val1 . $opt . $val2);
}
Mohammad
  • 21,175
  • 15
  • 55
  • 84
zaidysf
  • 492
  • 2
  • 14
0

well yo can use a very basic approach for your program like

switch ($opt) {
    case "+":
    echo  $val1 + $val2;
    break;
   case "-":
    echo  $val1 - $val2;;
    break;
   case "*":
    echo  $val1 * $val2;;
    break;
    ...
   default:
    code to be executed if $opt is different from all labels;
}
Muhammad Usman
  • 10,039
  • 22
  • 39
0

You could use eval for this purpose, but validate your data carefully. As this advise in manual states:

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

function cal($val1 , $opt , $val2)
{
    if (!is_numeric($val1)) return false;
    if (!is_numeric($val2)) return false;
    if (!in_array($opt, ["+", "-", "*", "/"])) return false;

    return eval("return $val1 $opt $val2;");
}

echo cal(3.2, "*", 4.2);

Live example

hlscalon
  • 7,304
  • 4
  • 33
  • 40