douzhi2988
2011-01-11 20:59
浏览 47
已采纳

is_null($ var)和($ var === null)之间有什么区别?

Is there any difference between this...

if (is_null($var)) {
    do_something();
}

and this?

if ($var === null) {
    do_something();
}

Which form is better when checking whether or not a variable contains null? Are there any edge cases I should be aware of? (I initialize all my variables, so nonexistent variables are not a problem.)

图片转代码服务由CSDN问答提供 功能建议

这个...

 之间是否有任何区别 (is_null($ var)){
 do_something(); 
} 
   
 
 

这个?

  if($ var === null){
 do_something(); 
} 
   
 
 

检查变量是否包含null时哪种形式更好 ? 我应该注意哪些边缘情况? (我初始化所有变量,因此不存在的变量不是问题。)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • doutan1875 2011-01-11 21:01
    已采纳

    Provided the variable is initialized (which you did indicate - though I'm not 100% sure if this matters in this context or not. Both solutions might throw a warning if the variable wasn't defined), they are functionally the same. I presume === would be marginally faster though as it removes the overhead of a function call.

    It really depends on how you look at your condition.

    === is for a strict data comparison. NULL has only one 'value', so this works for comparing against NULL (which is a PHP constant of the null 'value')

    is_null is checking that the variable is of the NULL data type.

    It's up to you which you choose, really.

    打赏 评论
  • doufei1988 2011-01-11 21:00

    Both are exactly same, I use is_null because it makes my code more readable

    打赏 评论
  • dongza6247 2011-01-11 21:01

    I would use the built in PHP function over the operator comparison every time.

    打赏 评论
  • dongpaozhi5734 2011-01-11 21:09

    is_null($var) is about 14 times slower than $var===null... 37.8 ms vs. 2.6 ms.

    But actually I don't know why.

    打赏 评论
  • doukan5332 2011-01-11 21:09

    I've just run a quick benchmark, testing a million iterations of each. is_null took 8 seconds to complete; === null took 1.

    So a call to is_null is 0.000007s slower than a call to === on my computer.

    I'd find something more useful to optimise.


    My code:

    <?php
    
    $start = time();
    $var = null;
    
    for ($i = 1000000; $i--; ) {
        is_null($var);
    }
    
    echo time() - $start;
    
    $start = time();
    
    for ($i = 1000000; $i--; ) {
        $var === null;
    }
    
    echo time() - $start;
    
    打赏 评论
  • douchi0638 2011-01-11 21:30

    If it seems redundant for php to have so many is_foo() type functions, when you can just use a standard comparison operators, consider programatically called functions.

    $arrayOfNullValues = array_filter($myArray, 'is_null');
    
    打赏 评论
  • duanhuau567787 2013-03-25 04:02

    is true

    is false

            | isset   | is_null | ===null | ==null  | empty   |
    |-------|---------|---------|---------|---------|---------|
    |  null |    ❌   |    ✅   |    ✅   |    ✅  |    ✅   |
    |  true |    ✅   |    ❌   |    ❌   |    ❌  |    ❌   |
    | false |    ✅   |    ❌   |    ❌   |    ✅  |    ✅   |
    |     0 |    ✅   |    ❌   |    ❌   |    ✅  |    ✅   |
    |     1 |    ✅   |    ❌   |    ❌   |    ❌  |    ❌   |
    |    \0 |    ✅   |    ❌   |    ❌   |    ❌  |    ❌   |
    | unset |    ❌   |    ✅   |    ✅   |    ✅  |    ✅   |
    |   ""  |    ✅   |    ❌   |    ❌   |    ✅  |    ✅   |
    

    Summary:

    打赏 评论

相关推荐 更多相似问题