3

I have 3 variables and a formula that trusted users need to be able to define via a CMS. This formula will change over time and the value of the variables come from a database.

How can I work out the answer to the calculation? I assume eval is relevant but can't quite get it to work

$width = 10;
$height = 10;
$depth = 10;

$volumetric = '(W*H*D)/6000';

$volumetric = str_replace('W', $width, $volumetric);
$volumetric = str_replace('H', $height, $volumetric);
$volumetric = str_replace('D', $depth, $volumetric);

eval($volumetric);

This gives me:

Parse error: parse error in /path/to/vol.php(13) : eval()'d code on line 1
10
  • 1
    Why you just write $volumetric = ($width * $height * $depth) / 6000. Make it a function and call it everywhere you need. So if it will change, you will just change one function. Commented Jul 28, 2016 at 12:06
  • Won't work. Non technical users will need to update the formula via a CMS so I need to be able to use variables I have pulled from my database ($width, $height, $depth) and apply this to the current formula Commented Jul 28, 2016 at 12:07
  • 1
    You need to be very careful when taking users input and using it with eval, you must sanitise the input or you will leave yourself wide open to being hacked. Commented Jul 28, 2016 at 12:09
  • 2
    Ah, but it does work if I add a ; to the end of the code but within eval ...... eval('$result = '.$volumetric.';'); Commented Jul 28, 2016 at 12:14
  • 2
    Have you tried this answer from a similar question : stackoverflow.com/a/16071456 ? Commented Jul 28, 2016 at 12:14

3 Answers 3

1

You need to be extremely careful with eval as you're giving people access to run commands directly on the server. Make sure to read the documentation thoroughly and understand the risks.

That said, you need to assign the result to a variable. You can tidy up what you're doing too, you only need one str_replace. Try this:

$width = 10;
$height = 10;
$depth = 10;

$volumetric = '(W*H*D)/6000';
$volumetric = str_replace(['W', 'H', 'D'], [$width, $height, $depth], $volumetric);

eval("\$result = $volumetric;");
echo $result;
Sign up to request clarification or add additional context in comments.

Comments

0

Eval was the correct way to go ... my correct code is:

$width = 60;
$height = 60;
$depth = 60;

$volumetric = '(W*H*D)/6000';

$volumetric = str_replace('W', $width, $volumetric);
$volumetric = str_replace('H', $height, $volumetric);
$volumetric = str_replace('D', $depth, $volumetric);

eval('$result = '.$volumetric.';');

echo $result;

Comments

0

Your starting point is true. If you don't want to use or code complex parsers, eval is best choice. But, eval converts given string to PHP code. So, basically what you are trying to that is;

$width = 10;
$height = 10;
$depth = 10;

$volumetric = '(W*H*D)/6000';

$volumetric = str_replace('W', $width, $volumetric);
$volumetric = str_replace('H', $height, $volumetric);
$volumetric = str_replace('D', $depth, $volumetric);

(10*10*10)/600;

So it outputs error. You should assign this equation to variable. Correct way is;

eval('$result = ('.$volumetric.');');

or

eval("\$result = ({$volumetric});")

Also I want to add something. Be careful! while using eval.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.