如何在IF语句中用值运算符语句实现变量?
if ($dcustomer['mix_max']=='MIN') {
$operator=">=";
} elseif ($dcustomer['mix_max']=='MAX') {
$operator="<=";
}
if ($djml['Tamount'] ."$operator". $dcustomer['limit_maksimal']){
}
答案 0 :(得分:0)
你不能(好吧,如果你使用了eval()就可以了,出于显而易见的原因应该避免使用它。)
但乘数可以达到同样的效果:
switch ($dcustomer['mix_max']) {
case 'MIN':
$multiplier = 1;
break;
case 'MAX':
$multiplier = -1;
break;
default:
// Always prepare for the unknown :-)
throw new \Exception("Unknown operator");
}
if ($djml['Tamount']*$multiplier >= $dcustomer['limit_maksimal']*$multiplier) {
...
}
但我认为它会更清晰,更灵活:
$condition = null;
switch ($dcustomer['mix_max']) {
case 'MIN':
$condition = ($djml['Tamount'] >= $dcustomer['limit_maksimal']);
break;
case 'MAX':
$condition = ($djml['Tamount'] <= $dcustomer['limit_maksimal']);
break;
default:
// Always prepare for the unknown :-)
throw new \Exception("Unknown operator");
}
if ($condition) {
...
}
答案 1 :(得分:0)
<?php
$dcustomer = array("mix_max"=>"MIN","limit_maksimal"=>2);
$djml = array("Tamount"=>"");
if ($dcustomer['mix_max']=='MIN') {
$operator=">=";
} elseif ($dcustomer['mix_max']=='MAX') {
$operator="<=";
}
echo $djml['Tamount'] .$operator. $dcustomer['limit_maksimal'];