3

I have set these variables in PHP:

$settings["operator"] = '/';
$settings["figure"] = '0.6';

I then want to use the above on another variable to work out a calculation, which will end up being:

$total = ($var->price / 0.6);

I tried this:

$total = ($var->price $settings["operator"] $settings["figure"]);

But I'm getting an error because the code is not correct:

Parse error: syntax error, unexpected '$settings' (T_VARIABLE)'

How can I use these variables to create my calculation?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
charlie
  • 415
  • 4
  • 35
  • 83
  • You can use a `switch` to determine what to do based on the value of `$settings["operator"]`. Something like `switch($settings["operator"]){ case '/': $total = ($var->price / 0.6); break; case '*': $total = ($var->price * 0.6); break; }` – gen_Eric May 20 '16 at 21:26
  • 1
    [How to make a calculator in PHP](http://stackoverflow.com/questions/12692727/how-to-make-a-calculator-in-php) – Mark Baker May 20 '16 at 21:26
  • I feel like the php side of s.o. is full of duplicate questions. – Sam Orozco May 20 '16 at 22:54

4 Answers4

3

You will have to make switch or something similar.

switch($settings["operator"]){
  case "/":
    $total = $var->price / $settings["figure"];
    break;

 case "+";
   $total = $var->price + $settings["figure"];
   break;
 //add others under here
}
Maantje
  • 1,781
  • 2
  • 20
  • 33
1

There is absolutely nothing wrong with eval(). It is great for many things, you just have to know how to use it safely. I wouldn't use a switch as it's not as flexible:

$allowed = array('/', '*', '-', '+');

if(in_array($settings["operator"], $allowed) && is_numeric($settings["figure"])) {
    eval('$result = ' . $var->price . $settings["operator"] . $settings["figure"] . ';');
}
echo $result;

Or maybe more readable:

 eval("\$result = {$var->price} {$settings['operator']} {$settings['figure']};");
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

See this answer by Kristopher:

[...] that syntax isn't available. The best you could do would be an eval(), which would not be recommended, especially if the $e came from user input (ie, a form) [...]

Rather use switch as he suggested:

$settings["operator"] = '/';
$settings["figure"] = '0.6';

switch ( $settings["operator"] ) {
  case '/':
    $total = $var->price / $settings["figure"];
    break;

// ...
}
Community
  • 1
  • 1
Yalon Esneck
  • 133
  • 8
0

PHP doesn't support variable operators in this way. It's essentially trying to handle the following $total = (number string string) hence the syntax error.

PHP does support variable functions so you could try the following :

//poke around the PHP manual and you will find functions that do this  kind of thing.
function doDiv($a,$b) {
    return ($a/$b):
}

$settings['someMathFunction'] = 'doDiv';
$settings['figure] = 0.6;

$total = $settings['someMathFunction']($var->price, $settings['figure']);

This is pretty horrible code though. Is there ever going to be a case where you aren't going to divide? If not then Y.A.G.N.I

Jon Ashdown
  • 176
  • 4
  • 11