13

Say I have a string, $char. $char == "*".

I also have two variables, $a and $b, which equal "4" and "5" respectively.

How do I get the result of $a $char $b, ie 4 * 5 ?

Thanks :)

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
Fela Maslen
  • 2,022
  • 3
  • 25
  • 46

7 Answers7

21

You can use eval() as suggested by @konforce, however the safest route would be something like:

$left = (int)$a;
$right = (int)$b;
$result = 0;
switch($char){

  case "*":
    $result = $left * $right;
    break;

 case "+";
   $result = $left + $right;
   break;
// etc

}
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
10

safest method is a switch construct:

function my_operator($a, $b, $char) {
    switch($char) {
        case '=': return $a = $b;
        case '*': return $a * $b;
        case '+': return $a + $b;
        etc...
    }
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
6

The easiest but most dangerous method is to use eval.

$c = eval("return $a $char $b;");
Matthew
  • 47,584
  • 11
  • 86
  • 98
  • I'd rather not use eval as the values come from user input. – Fela Maslen Apr 25 '11 at 16:32
  • @konforce: At last all we have to use the code in production they why we suggest eval – Shakti Singh Apr 25 '11 at 16:33
  • @Fela, you should _always_ be validating the user input whether you use `eval` or not. If you are validating that `$a` and `$b` are proper numbers and that `$char` is a proper operation then `eval` will be safe. That said, I would still not use `eval` on a public site. – Matthew Apr 25 '11 at 16:34
  • 1
    if it's a local project, there is no reason not to use `eval`. – fingerman Apr 25 '11 at 16:35
  • I'm not using eval, I'm using switches with different operators. All user input is sanitised of course. – Fela Maslen Apr 25 '11 at 16:57
2

Another way to avoid creating a switch/case, could be:

<?php 
// Doesn't matter if $a and $b are integers or strings
$a = 100;
$b = 1000;
$operator = ">";

$operations = [
  '='  => $a == $b,
  '>'  => $a > $b,
  '>=' => $a >= $b,
  '<'  => $a < $b,
  '<=' => $a <= $b,
  '<>' => $a <> $b,
];

// It will get the value of $a > $b and the result will be false
$operations[$operator]
?>

Finally if you prefer, you can move the $operations array, to a function to improve the readability code.

<?php 
// Doesn't matter if $a and $b are integers or strings
$a = 100;
$b = 1000;
$operator = ">";

// It will get the value of $a > $b and the result will be false
$result = getOperationResult($a, $b, $operator);

function getOperationResult(string|int $a, string|int $b, string $operator): bool
{
    $operations = [
      '='  => $a == $b,
      '>'  => $a > $b,
      '>=' => $a >= $b,
      '<'  => $a < $b,
      '<=' => $a <= $b,
      '<>' => $a <> $b,
    ];

    return $operations[$operator];
}
?>
Salvation
  • 21
  • 4
1

take a look at the eval() function. you will need to build a proper php command and run inside the eval() to extract out the result.

Alan Barber
  • 983
  • 5
  • 15
1

You can do with eval however I would not suggest using eval.

If there is case operator can by anything you should check what operator is before using

switch($char)
{
  case '*':
    $result= $a * $b;
    break;

  case '+':
    $result= $a + $b;
    break;
}
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
0
<?php
$a = 'alex';
$b = "alex";
$c = "==";
function abc($a,$b,$c){
  $d = 'return ($a '.$c.' $b) ? true : false;';
  return eval($d);
}
if(abc($a,$b,$c)){
  echo "condition true";
}else{
  echo "condition false";
}
// echo $e;
?>