PHP 使用变量计算
PHP calculation using variables
我有 3 个变量和一个公式,受信任的用户需要能够通过 CMS 进行定义。这个公式会随着时间的推移而改变,变量的值来自数据库。
如何计算出计算的答案?我假设 eval 是相关的,但不能完全让它工作
$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);
这给了我:
Parse error: parse error in /path/to/vol.php(13) : eval()'d code on line 1
Eval 是正确的方法...我的正确代码是:
$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;
您需要非常小心 eval
as you're giving people access to run commands directly on the server. Make sure to read the documentation 并彻底了解风险。
也就是说,您需要将结果分配给一个变量。你也可以收拾你正在做的事情,你只需要一个str_replace
。试试这个:
$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;
你的出发点是正确的。如果您不想使用或编写复杂的解析器,eval 是最佳选择。但是,eval 将给定的字符串转换为 PHP 代码。所以,基本上你正在尝试的是;
$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;
所以输出错误。您应该将此等式分配给变量。正确的方法是;
eval('$result = ('.$volumetric.');');
或
eval("$result = ({$volumetric});")
另外我想补充一点。 小心! 使用 eval 时。
我有 3 个变量和一个公式,受信任的用户需要能够通过 CMS 进行定义。这个公式会随着时间的推移而改变,变量的值来自数据库。
如何计算出计算的答案?我假设 eval 是相关的,但不能完全让它工作
$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);
这给了我:
Parse error: parse error in /path/to/vol.php(13) : eval()'d code on line 1
Eval 是正确的方法...我的正确代码是:
$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;
您需要非常小心 eval
as you're giving people access to run commands directly on the server. Make sure to read the documentation 并彻底了解风险。
也就是说,您需要将结果分配给一个变量。你也可以收拾你正在做的事情,你只需要一个str_replace
。试试这个:
$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;
你的出发点是正确的。如果您不想使用或编写复杂的解析器,eval 是最佳选择。但是,eval 将给定的字符串转换为 PHP 代码。所以,基本上你正在尝试的是;
$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;
所以输出错误。您应该将此等式分配给变量。正确的方法是;
eval('$result = ('.$volumetric.');');
或
eval("$result = ({$volumetric});")
另外我想补充一点。 小心! 使用 eval 时。