PHP - 我可以在if语句中使用函数作为等式吗?

时间:2013-04-11 08:33:54

标签: php function if-statement

我有一个我想要运行的报警系统,但根据统计数据,它需要报警大于或小于上周这个时间的统计数据。 if语句只是:

if ((($past / 100) * 75) > $present) 

但我需要在某些条件下将大人物转移到一个小人物身上。功能是最好的方法吗?我无法理解他们在这方面的工作方式。一个例子就是好的,我只能找到通用打印功能等等。

3 个答案:

答案 0 :(得分:2)

function compare($a,$b,$operator)
{
 if($operator==">")
   {
    return ($a>$b);
   }
 else if($operator=="<")
   {
    return ($a<$b);
   }
}

检查其是否更大

if (compare(($past / 100) * 75),$present,">") 
{

}

检查其是否更小

if (compare(($past / 100) * 75),$present,"<") 
{

}

答案 1 :(得分:2)

我认为您的if语句可以写成

if ( 
    (!$certain_condition && ($past / 100 * 75) > $present) 
    || ($certain_condition && ($past / 100 * 75) < $present) 
) 

鉴于某些条件的结果可以存储在变量$certain_condition

答案 2 :(得分:0)

将它放入一个功能中并按照你想要的方向进行输入。

function comparison($past, $present, $gt = true){

if($gt){
  return  ( ( ($past / 100) * 75) > $present) ? true : false;
}else{
  return  ( ( ($past / 100) * 75)  <  $present) ? true : false;
}

}

// debug
// var_dump(comparison($past, $present, true));

像这样使用 - 它返回true或false。

$past = 100;
$present = 80;

// usage eg
if( comparison($past, $present) ){
// start running some other process in gt> mode

}