doulu3399 2016-08-14 18:35
浏览 34

通过函数参数传递比较运算符

I'd like my function to have a comparison operator as an argument (or preferably, as part of an argument such as >0 ):

Instead of

function comps($a,$b){
    if ($a > $b)
      echo "This works.";
}
comps(1,0);

I'd like to be able to do something like

function comps($a,$b){
    if ($a $b)
      echo "This works.";
}
comps(1,'>0');

I've been banging my head agains the wall on this for a while. I've tried different iterations of:

"/>0"
'>' . 0
(string)> 0

as well as trying the comparison operator as a third argument.

Actual use is:

function mysort($key,$match){
$temp_array = array();
global $students;

foreach ($students as $row) {
    if($row[$key] > $match ) 
    $temp_array[]= $row;
    }

foreach ($temp_array as $row) {
    echo $row['email'] . ', ';
    }
}
mysort('suzuki', '0'); 

Thanks

  • 写回答

2条回答 默认 最新

  • drgawfsf1069 2016-08-14 18:58
    关注

    You can make a return type function and use as you want.

    I'll Make a function to you as Below:

    <?php
        function getCond($a, $b, $both) {
            $data = false;
            if($both) {
                if($a == b) {
                    $data = true;
                }
            }else {
                 if($a > $b) {
                     $data = true;
                 }
            }
            return $data;
        }
    
     /*  Use Your Function  */
        if (getCond(10, 5, true)) {      // condition for 10 == 5 and according to passed values you get false result in condition 
            echo "you result";
        } else if (getCond(10, 5, false)) {  // condition for 10 > 5 and according to passed values you get false result in condition
            echo "your result";
        } else if (getCond(6, 5, false)){    // condition for 6 > 5 and according to passed values you get true result in condition
            echo "your result";
        } 
    ?>
    

    You also can modify this function as you want :)

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统