dsjklb0205 2013-11-14 03:26
浏览 77

我是否需要在括号中包含PHP内置函数?

Do I need to enclose PHP built-in functions in parentheses?

Are these the most proper ways?

(empty($UrlArray[1])), (is_numeric($UrlArray[1])), (strlen($UrlArray[1]) == 4)

Or, are these the most proper ways?

empty($UrlArray[1]), is_numeric($UrlArray[1]), strlen($UrlArray[1]) == 4

This one works but I'm wondering if I have too many parentheses.

( ( (empty($UrlArray[1])) OR ( (!empty($UrlArray[1])) AND (is_numeric($UrlArray[1])) AND (strlen($UrlArray[1]) == 4) ) ) AND (count($UrlArray) < 3) )

If I omit the parentheses that enclose the PHP built-in functions the code snippet still works.

( ( empty($UrlArray[1]) OR ( !empty($UrlArray[1]) AND is_numeric($UrlArray[1]) AND strlen($UrlArray[1]) == 4 ) ) AND count($UrlArray) < 3 )

I have always enclosed the PHP built-in functions in parentheses. But I am just wondering if that is the best way.

Thanks.

  • 写回答

1条回答 默认 最新

  • douweicheng5532 2013-11-14 03:28
    关注

    No. You don't need to wrap functions in parentheses. In fact, if I had to maintain code like that, it would probably drive me crazy.

    Seems like it would only add to confusion, especially when you already have a lot of parentheses. It will make them more difficult to match up.

    In your first example however, you have this:

    (strlen($UrlArray[1]) == 4)
    

    IMO, this one is fine. I would probably wrap the entire comparison in parentheses like this, but I would NOT do something like this:

    ((strlen($UrlArray[1])) == 4)
    

    I might also format it for readability:

    if (
        count($UrlArray) < 3
        && (
            empty($UrlArray[1])
            || (
                !empty($UrlArray[1])
                && is_numeric($UrlArray[1])
                && strlen($UrlArray[1]) == 4
            )
        )
    ) {
        // do something
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器