eval - PHP calculation using variables -


i have 3 variables , formula trusted users need able define via cms. formula change on time , value of variables come database.

how can work out answer calculation? assume eval relevant can't quite 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 

you need extremely careful eval you're giving people access run commands directly on server. make sure read documentation thoroughly , understand risks.

that said, need assign result variable. can tidy you're doing too, need 1 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; 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -