douhao2548 2010-07-24 21:13
浏览 86

测试和回声没有重复?

This is a minor thing, but it's been bugging me for a while. I've wracked my brain for a way to write statements like this without any repetition of code. For example:

echo isset($array[0])? $array[0]: 'not set';
$var = empty($other_var)? '$other_var not set': $other_var;

Is there some sort of test-and-return (for the former) or test-and-set (for the latter) operator I don't know about? This may seem like a minor point, but the duplication seems unnecessary and can lead to very long lines that can complicate maintenance. Consider:

$another_var = array_key_exists($array[Utility::FindIndex($username)][Constants::App_CRITERION], $haystack[NthDimension('my dimensional indicator')])? $array[Utility::FindIndex($username)][Constants::App_CRITERION], $haystack[NthDimension('my dimensional indicator')]: 'not set';

Yes, yes, the above line is totally contrived but it's not unthinkable that something like this could occur. It just seems strange to me that there isn't a way to test something and assign it's value (if true) without repetition without repetition.

  • 写回答

2条回答 默认 最新

  • dqq9695 2010-07-24 21:57
    关注

    I think in PHP 6 there was a function planned called issetor or something similar. But I can't remember the name. And PHP 6 is dead either way.

    So, write it yourself:

    function issetor(&$var, $default) {
        return isset($var) ? $var : $default;
    }
    
    echo issetor($_GET['me'], 'you');
    

    If you want to make it even more abstract, look at this:

    function isor(&$var, $default, $condition) {
        if (!is_callable($condition)) {
            throw new InvalidArgumentExpression('condition not callable!');
        }
    
        return $condition($var) ? $var : $default;
    }
    
    // this is equivalent to issetor($_GET['me'], 'you');
    echo isor($_GET['me'], 'you', function(&$var) { return isset($var); });
    
    // but you may use a more complicated thing here, too:
    echo isor($_GET['me'], 'you', function($var) use($allowed) { return in_array($var, $allowed); });
    // this is equivalent to:
    echo in_array($_GET['me'], $allowed) ? $_GET['me'] : 'you';
    // now the "normal" version is still shorter. But using isor allows you to store often used $condition closures in variables. For example, if you want to check if several values are in an array, you could write:
    $isAllowed = function ($var) use ($allowed) {
        return in_array($var, $allowed);
    };
    $a = isor($a, 'default', $inAllowed);
    $b = isor($b, 'default', $inAllowed);
    $c = isor($c, 'default', $inAllowed);
    $d = isor($d, 'default', $inAllowed);
    

    If you want to pass additional variables to your condition function without always useing closures you may add another argument. (Note I didn't use an argument array and call_user_func_array, because you may not pass per reference using it, but obviously you may extend the code so it does so.)

    function isor(&$var, $default, $condition, $addArgument = null) {
        if (!is_callable($condition)) {
            throw new InvalidArgumentExpression('condition not callable!');
        }
    
        return $condition($var, $addArgument) ? $var : $default;
    }
    
    // the above in_array condition:
    echo isor($a, 'default', 'in_array', $allowed);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题