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?

3
  • 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
    Commented May 20, 2016 at 21:26
  • 1
    How to make a calculator in PHP
    – Mark Baker
    Commented May 20, 2016 at 21:26
  • I feel like the php side of s.o. is full of duplicate questions.
    – Sam Orozco
    Commented May 20, 2016 at 22:54

4 Answers 4

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
}
2
  • Just FYI, your case is / but you use * Commented May 20, 2016 at 21:53
  • @AbraCadaver Ah typed to fast, edited it! thanks.
    – Maantje
    Commented May 22, 2016 at 16:02
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']};");
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;

// ...
}
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.